473,558 Members | 2,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.soc ket(socket.AF_I NET,socket.SOCK _STREAM)
print 'trying to connect...'
sock.connect((' 127.0.0.1',9999 ))
print 'connected!'
# executed on windows
>C:\Python25\py thon.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.soc ket(socket.AF_I NET,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\py thon.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 2393
On 2008-07-09, Mirko Vogt <li***@nanl.dew rote:
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.dew rote:
>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
16598
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 @ port 21567: #!/usr/bin/python2 from socket import * from time import time,ctime HOST ='127.0.0.1' PORT = 21567
3
2642
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 through a socket. Here is the code : <server> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.settimeout(5.0)
6
4097
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 transfers to a server): ThreadStart pseudo code: Connect Receive response Send Connect ACK
10
5582
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 some data arrives). I think that's wrong, but I don't want to argue that. I would like to create a subclass of socket that fixes the problem. Ideally, something like: class...
8
5984
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 "localhost" to the desired value: D:\AUT_TEST\workspace\JyFIT>jython fit/JyFitServer2.py localhost 1234 23 localhost Traceback (innermost last): File "fit/JyFitServer2.py", line 146, in ?
9
3590
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 packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
2
6863
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 Connections once established will not be closed unless there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server"...
3
4276
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 sends to the same IP address and port, the receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
3
2224
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 compatible with Python 2.3 and 2.4 I'd like to know how could I determine the family of a socket.socket instance which may be AF_INET or AF_INET6. Is there some kind of getsockopt() directive I could use?...
2
2082
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 message and response to it. 3) the client get the server message and send another message. 4) finally, the server receive the message and close the connection.
0
7629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7835
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8061
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5455
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.