How to stop a python socket.accept() call?

python, sockets

Solution

You have several options here:

- Close the listening socket from another thread - the `accept()` will raise an exception if it fails.

- Open a local connection to the listening socket - that makes the `accept()` return by design.

- Use an accept mechanism that can block on more than one synchronization object so that the wait can be signaled to return without a connection.

- Use a non-blocking alternative to `accept()`, (async like `AcceptEx()` and overlapped IO on Windows).

Problem

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.

Original source