471,873 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 3385
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Matthew Barnes | last post: by
5 posts views Thread by grant | last post: by
6 posts views Thread by g.franzkowiak | last post: by
3 posts views Thread by Eric Jacoboni | last post: by
2 posts views Thread by Pieter Rautenbach | last post: by
4 posts views Thread by OhKyu Yoon | last post: by
2 posts views Thread by brnstrmrs | last post: by
reply views Thread by Ping Zhao | last post: by
reply views Thread by zermasroor | last post: by

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.