Why is this server crashing?

1
from socket import*


Host = "localhost"
Port = 255

sockobj = socket(AF_INET, SOCK_STREAM)
sockobj.bind((Host, Port))
sockobj.listen(5)

while True:

    conexão, endereço = sockobj.accept()
    print('Server conectado por', endereço)

while True:

    data = conexão.recv(1024)

    if not data:break







        conexão.close() 

Python simply hangs and prints no errors

    
asked by anonymous 06.01.2017 / 15:25

1 answer

3

Corrected, I should start by saying that variables with special characters (ç, ã ...) are not at all advised, and with capital letters in the first letter is also not conventional.

You were also doing two cycles while True: without any break in the first one, we all know what this results in, when in fact it is only in the second (second) that you need to be "eternally" thing you can do is use context manager to "open the sockets" this way in addition to being currently the most advised, also need to close the sockets, as well as its use in opening a file:

from socket import*


host = "localhost"
port = 9003

with socket(AF_INET, SOCK_STREAM) as sockobj:
    sockobj.bind((host, port))
    sockobj.listen(5)

    conexao, endereco = sockobj.accept() # esperar ate que cliente se ligue
    print('Server conectado por', endereco)

    while True:
        data = conexao.recv(1024)
        if not data:break

Note that when I was testing one of the errors you gave me:

  

ConnectionRefusedError: [Errno 111] Connection refused

This because of the port that was being used by any other process

    
06.01.2017 / 15:47