getting socket.bind() exception, but no actual error

Steve Holden steve at
Mon Nov 1 22:21:35 CET 2004

Clarence Gardner wrote:
> I've got a problem that I don't see how to program around. A socket server
> that was running fine, today started getting an exception from the bind()
> call (errno 22, Invalid argument) and yet the bind had actually worked.
> This is apparent from this code: it prints the error message, but then 
> breaks out of the loop and the listen() and accept() calls work normally.
> How can one handle a function that is both working and raising an
> exception?
>     sockobj=socket(AF_INET,SOCK_STREAM)
>     while True:
>         try:
>             sockobj.bind(('',4321))
>         except socketerror,e:
>             print e
>             if str(e).find('in use') == -1:
>                 break
>             print '.'
>             time.sleep(1)
>     sockobj.listen(5)

Is it possible you already have a process bound to that port, and that 
process is handling the incoming connections?

Otherwise it sounds a little bizarre. In fact even my own attempt at 
explaining doesn't ring true because on Win2k, at least, a second bind 
will raise (10048, 'Address already in use'), and I get a similar error 
on Linux. Rats.

I take it this code has been copied and pasted? For example, if the host 
were a single space rather than an empty string /that/ might cause this 
problem. Sorry I can't be more help.

Holden Web LLC +1 800 494 3119

More information about the Python-list mailing list