I am a newbie in python sockets and am really troubled by the stubbornness of the socket.accept()
method. I really need a way of ending a socket.accept()
method or any other alternative to socket.accept()
which runs one time only.
To close the connection, break the while loop. Garbage collection will remove the thread or process but join will ensure none get left behind. Persistent sockets close when the user closes them or they timeout. Non-persistent, like static webpages will close after they've sent the information.
The accept() method is called on a TCP based server socket. When connect() is called at the client side with the IP address and port number of the server, the connect request is received with the accept() call at the server side.
It is only ever necessary to shutdown a socket for writing if (1) you have forked the process and definitely want to send the FIN now, or (2) you are engaging in a mutual read-to-EOS protocol such that both peers close at the same time. Otherwise close() is sufficient. The Python documentation should be corrected.
A new Python socket by default doesn't have a timeout. Its timeout defaults to None. Not setting the connection timeout parameter can result in blocking socket mode. In blocking mode, operations block until complete or the system returns an error.
You have several options here:
accept()
will raise an exception if it fails.accept()
return by design.accept()
, (async like AcceptEx()
and overlapped IO on Windows).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With