473,466 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Cross-platform socket.getsockopt and struct.unpack (or socket timeout)?

M2Crypto has some old code that gets and sets socket timeouts in
http://svn.osafoundation.org/m2crypt.../Connection.py,
for example:

def get_socket_read_timeout(self):
return
timeout.struct_to_timeout(self.socket.getsockopt(s ocket.SOL_SOCKET,
socket.SO_RCVTIMEO, timeout.struct_size()))

The helper timeout module is here:
http://svn.osafoundation.org/m2crypt...SSL/timeout.py

I just noticed that this does not work on Windows. Here's a small test
program that demonstrates the issue using native and Cygwin Python
2.5.2, 32-bit Windows XP (no M2Crypto needed for this):

import struct
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
timeout_struct = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO,
struct.calcsize('ll'))
print "%d:'%s'" % (len(timeout_struct), timeout_struct)
print struct.unpack('ll', timeout_struct)

This outputs

4:' '
Traceback (most recent call last):
File "t.py", line 8, in <module>
print struct.unpack('ll', timeout_struct)
File "/usr/lib/python2.5/struct.py", line 87, in unpack
return o.unpack(s)
struct.error: unpack requires a string argument of length 8

It works fine on 32 and 64 bit Linux (Ubuntu 8.04).

The kicker is that I can't remember if I actually ever tested this on
Windows before.

This goes to a level of socket programming that I haven't done before.

Is there a cross-platform way of doing what the M2Crypto code is
attempting to do?

--
Heikki Toivonen
Sep 16 '08 #1
2 3514
Heikki Toivonen wrote:
Is there a cross-platform way of doing what the M2Crypto code is
attempting to do?
calling the socket object's settimeout() method?

</F>

Sep 16 '08 #2
Fredrik Lundh wrote:
Heikki Toivonen wrote:
>Is there a cross-platform way of doing what the M2Crypto code is
attempting to do?

calling the socket object's settimeout() method?
I would suspect that is not strictly equivalent to what the old code is
doing, though, because it seems you can set read and write timeouts
separately using the socket options directly.

--
Heikki Toivonen
Sep 16 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matthew Barnes | last post by:
I was wondering if there would be any interest in extending the struct.unpack format notation to be able to express groups of data with parenthesis. For example: >>> data =...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
6
by: g.franzkowiak | last post by:
Hello Everybody, I've read a pipe and store it in a object. My next step was the separation from 4 bytes with obj = string.join(list(dataObject) ==> '\x16 \x00 \x00 \x00' and the converting by...
3
by: Eric Jacoboni | last post by:
Hi, To experiment with unpacking, i've written a little C code which stores one record in a file. Then, i try to reread this file to unpack the record. Here's the struct of a record: ...
2
by: Pieter Rautenbach | last post by:
Hallo, I have a 64 bit server with CentOS 4.3 installed, running Python. $ uname -a Linux lutetium.mxit.co.za 2.6.9-34.ELsmp #1 SMP Thu Mar 9 06:23:23 GMT 2006 x86_64 x86_64 x86_64 GNU/Linux...
4
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.