472,347 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 2294
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chuck E. Cheese | last post by:
I need a php page to connect to a python created socket and sent and receive data. below is the python code which opens a socket on the localhost...
3
by: Thomas Hervé | last post by:
My problem is not really python specific but as I do my implementation in python I hope someone here can help me. I have two programs that talk...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet...
10
by: groups.20.thebriguy | last post by:
socket objects have a little quirk. If you try to receive 0 bytes on a blocking socket, they block. That is, if I call recv(0), it blocks (until...
8
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which...
3
by: Giampaolo Rodola' | last post by:
Hi there, since the socket.socket.family attribute has been introduced only in Python 2.5 and I need to have my application to be backward...
2
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.