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

Discussion in 'Python' started by Mirko Vogt, Jul 9, 2008.

  1. Mirko Vogt

    Mirko Vogt Guest

    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_STREAM)
    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_STREAM)
    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
    Mirko Vogt, Jul 9, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tech guru

    Different code behaviour in Unix & Windows

    tech guru, Apr 18, 2006, in forum: C Programming
    Replies:
    41
    Views:
    968
    Keith Thompson
    Apr 20, 2006
  2. Marco
    Replies:
    0
    Views:
    359
    Marco
    Mar 13, 2007
  3. Andy Chambers
    Replies:
    1
    Views:
    385
    Daniel Dyer
    May 14, 2007
  4. Laszlo Nagy
    Replies:
    1
    Views:
    4,823
    Mark Wooding
    Jan 27, 2009
  5. Jean-Paul Calderone
    Replies:
    0
    Views:
    966
    Jean-Paul Calderone
    Jan 27, 2009
Loading...

Share This Page