471,573 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

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 1953
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
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.