socket programming

Chris Angelico rosuav at gmail.com
Fri May 3 22:23:38 EDT 2013


On Sat, May 4, 2013 at 12:13 PM, Pedro <pedro at ncf.ca> wrote:
> First - this code constantly loops around an open socket. Is there a way to use something like an interrupt so I don't have to loop constantly to monitor the socket?

The accept() call should block. It's not going to spin or anything. If
you need to monitor multiple sockets, have a look at select().

> Second, if any part of the program fails execution (crashes) the port often remains open on my windows machine and the only way to close it that i know of is through task manager or by rebooting the machine. Is there an easy way around this problem ? If I don't close the port the program can't open it again and crashes.

It remains for a short time to ensure that there's no lurking
connections. You can bypass this check by setting the SO_REUSEADDR
option - lemme hunt that down in the Python docs, haven't done that in
Python for a while...

http://docs.python.org/3.3/library/socket.html#socket.socket.setsockopt

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

That should do the job.

ChrisA



More information about the Python-list mailing list