469,275 Members | 1,852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

how do I stop SocketServer()?

supposing that I have a server (an instance of SocketServer()) that
waits for a connection (ie is blocked in accept()) and in another
thread i want to stop the server, how do I do that?
Aug 27 '08 #1
2 1903
Alexandru Mosoi wrote:
supposing that I have a server (an instance of SocketServer()) that
waits for a connection (ie is blocked in accept()) and in another
thread i want to stop the server, how do I do that?
By setting a timeout on the socket using socket.settimeout, and then
periodically check for an abortion condition in the server thread before
re-accepting connections.

Diez
Aug 27 '08 #2
On Wed, 27 Aug 2008 18:44:46 +0200, "Diez B. Roggisch"
<de***@nospam.web.dewrote:
>Alexandru Mosoi wrote:
>supposing that I have a server (an instance of SocketServer()) that
waits for a connection (ie is blocked in accept()) and in another
thread i want to stop the server, how do I do that?

By setting a timeout on the socket using socket.settimeout, and then
periodically check for an abortion condition in the server thread before
re-accepting connections.
You can also poll for activity by using the select() call. For
example:

...
local_host = '' # Symbolic name meaning the local host

server_port = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_port.bind((local_host, local_port))
server_port.listen(1)

readsocks = []
readsocks.append(sync_port)

# 'halt' would be your stop condition, set elsewhere
while halt == False:
readables, writeables, exceptions = select(readsocks, [], [], 1)

# does the socket has a connection pending?
if server_port in readables:
# process client connection
client_conn, client_addr = server_port.accept()
...
Aug 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lebo | last post: by
3 posts views Thread by Olivier Hoarau | last post: by
3 posts views Thread by Ergin Aytac | last post: by
reply views Thread by Adil Hasan | last post: by
5 posts views Thread by missiplicity | last post: by
12 posts views Thread by Paul Rubin | last post: by
reply views Thread by Tomi Hautakoski | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.