473,804 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross-platform socket.getsocko pt 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.getsock opt(socket.SOL_ SOCKET,
socket.SO_RCVTI MEO, 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(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
timeout_struct = s.getsockopt(so cket.SOL_SOCKET , socket.SO_RCVTI MEO,
struct.calcsize ('ll'))
print "%d:'%s'" % (len(timeout_st ruct), 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 3539
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
1942
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 = struct.pack('iiii', 1, 2, 3, 4) >>> struct.unpack('i(ii)i', data) # Note the parentheses (1, (2, 3), 4)
5
13870
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: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD' # the above was printed using repr(xbuffer). # Note that int(0x13) = 19 which is exactly the length of the visible text #
5
5459
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 coming through fine when using (integer1,) = struct.unpack('l', line) except when line contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that
6
4525
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 value = struct.unpack('I', obj) generated the error "unpack str size does not match format" Unfortunately is len(obj) 7, but integer lengt 4.
3
5527
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: typedef struct { char nom;
2
2002
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 Consider the following two snippets, issuing a struct.unpack(...) using Python 2.3.4 and Python 2.5 respectively.
4
2805
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 something tdc = struct.unpack('=LiLiLiLi',self.lmf.read(32)) # do something
2
4403
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 = struct.unpack('h', testValue)
0
1624
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', self.__read(src, 2))) header = int(struct.unpack('1i', self.__read(src, 4)))
0
9711
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.