Why is this server crashing?

Asked

Viewed 59 times

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 crashes and prints no errors

  • Puts the code across sff. What is the error?

  • This is the code

  • Imports etc... "localhost is not defined", "ame 'AF_INET' is not defined"... etc

  • Does it lock... by bug or because it is eternally waiting for the customer to send a message? Note that your program handles one connection at a time, and as it stands there waiting, it may be that you just aren’t realizing that the fault is actually in the customer. Note also that your ties are not properly indented.. You have one. while infinite just to accept connections! (and, this is probably your problem: your server accepts the first connection, prints the message "connected..." and again accepts connections, getting "apparently" stuck)

  • 1

    Hangs without printing errors? is because the server is working?

1 answer

2


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

You were also doing two cycles while True: without any break in the first, we all know what this results in, when in fact it is only in the last (second) that you need it to be "eternally" running, another thing you can do is use context manager, for "opening sockets" in this way as well as being currently the most advised, also excuses from closing the sockets, such as their 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 mistakes you gave me was:

Connectionrefusederror: [Errno 111] Connection refused

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

  • "I must begin by saying that variables with special characters (ç, ã ...) is not at all advised [...]". Totally true. Among other difficulties, think that someone who is going to use or maintain your code may not have a keyboard with cedilla (ç).

  • 1

    Of course @Luizvieira, just be from another country to not understand it hehe

  • 1

    on Linux (and probably on Windows too) ports smaller than 1024 can be used as a server only by privileged processes (root, Administrator); thus in the OP example in addition to all errors there was the question of port 255 used

  • exact @Joséx, correct

Browser other questions tagged

You are not signed in. Login or sign up in order to post.