467,083 Members | 1,230 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

socket-module: different behaviour on windows / unix when a timeoutis set

Hey,

it seems that the socket-module behaves differently on unix / windows when a timeout is set.
Here an example:

# test.py

import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_STRE AM)
print 'trying to connect...'
sock.connect(('127.0.0.1',9999))
print 'connected!'
# executed on windows
>C:\Python25\python.exe test.py
trying to connect...
Traceback (most recent call last):
File "test.py", line 4, in <module>
sock.connect(('127.0.0.1',9999))
File "<string>", line 1, in connect
socket.error: (10061, 'Connection refused')
>

# executed on linux

$ python test.py
trying to connect...
Traceback (most recent call last):
File "test.py", line 4, in <module>
sock.connect(('127.0.0.1',9999))
File "<string>", line 1, in connect
socket.error: (111, 'Connection refused')
$
Even if the error-codes are different both raise an socket.error with the message 'Connection refused' - good so far.
Now I will change the code slightly - to be precise I set a timeout on the socket:
# test.py

import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_STRE AM)
sock.settimeout(3.0) # <----------------------------------------------------------
print 'trying to connect...'
sock.connect(('127.0.0.1',9999))
print 'connected!'
# executed on linux

$ python test.py
trying to connect...
Traceback (most recent call last):
File "test.py", line 5, in <module>
sock.connect(('127.0.0.1',9999))
File "<string>", line 1, in connect
socket.error: (111, 'Connection refused')
$
# executed on windows
>C:\Python25\python.exe test.py
trying to connect...
connected!
>

The code executed by python running on windows does *not* raise the exception anymore.
The Linux does as expected.

Is that behaviour common or even documented? Found nothing.

It took me lot's of time to figure this out, because there was no exception which was raised when testing for open / ports.

When I try to read from the socket (e.g. on port 9999 on which nothing runs) I get a timeout after the via settimeou() specified value.

Thanks in advance,

Mirko
Jul 9 '08 #1
  • viewed: 2101
Share:
2 Replies
On 2008-07-09, Mirko Vogt <li***@nanl.dewrote:
Is that behaviour common or even documented? Found nothing.
Second sentence in the socket module documentation:

Note: Some behavior may be platform dependent, since calls are made to the
operating system socket APIs.
So yes, what you found is probably how it should behave.

Albert

Jul 10 '08 #2
On 2008-07-10, A.T.Hofkamp <ha*@se-162.se.wtb.tue.nlwrote:
On 2008-07-09, Mirko Vogt <li***@nanl.dewrote:
>Is that behaviour common or even documented? Found nothing.
Second sentence in the socket module documentation:

Note: Some behavior may be platform dependent, since calls are
made to the operating system socket APIs.

So yes, what you found is probably how it should behave.
I have a hard time believing that the phrase "platform
dependent" should be stretched to cover the OP's example where
the results are just plain _wrong_.

--
Grant Edwards grante Yow! I'm having BEAUTIFUL
at THOUGHTS about the INSIPID
visi.com WIVES of smug and wealthy
CORPORATE LAWYERS ...
Jul 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chuck E. Cheese | last post: by
3 posts views Thread by Thomas Hervé | last post: by
10 posts views Thread by groups.20.thebriguy@spamgourmet.com | last post: by
8 posts views Thread by Mark Fink | last post: by
3 posts views Thread by Giampaolo Rodola' | last post: by
2 posts views Thread by Ali Hamad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.