inet_aton and struct issue

D

David Bear

I found this simple recipe for converting a dotted quad ip address to a
string of a long int.

struct.unpack('L',socket.inet_aton(ip))[0]

trouble is when I use this, I get

struct.error: unpack str size does not match format

I thought ip addresses were unsigned 32 bit integers.

Is there a better way to take a dotted quad and convert it to a string
representation of an long int?
 
D

Diez B. Roggisch

David said:
I found this simple recipe for converting a dotted quad ip address to a
string of a long int.

struct.unpack('L',socket.inet_aton(ip))[0]

trouble is when I use this, I get

struct.error: unpack str size does not match format

I thought ip addresses were unsigned 32 bit integers.

Is there a better way to take a dotted quad and convert it to a string
representation of an long int?

Works for me:
>>> import socket
>>> import struct
>>> ip = "127.0.0.1"
>>> struct.unpack('L',socket.inet_aton(ip))[0]
2130706433L


Diez
 
D

David Bear

Diez said:
David said:
I found this simple recipe for converting a dotted quad ip address to a
string of a long int.

struct.unpack('L',socket.inet_aton(ip))[0]

trouble is when I use this, I get

struct.error: unpack str size does not match format

I thought ip addresses were unsigned 32 bit integers.

Is there a better way to take a dotted quad and convert it to a string
representation of an long int?

Works for me:
import socket
import struct
ip = "127.0.0.1"
struct.unpack('L',socket.inet_aton(ip))[0]
2130706433L

I really wish it worked for me:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format

This is python packaged with Suse 9.3.
dir(struct) ['__doc__', '__file__', '__name__', 'calcsize', 'error', 'pack', 'unpack']
print struct.__file__
/usr/lib64/python2.4/lib-dynload/struct.so

could I have a broken python?
 
D

David Bear

David said:
Diez said:
David said:
I found this simple recipe for converting a dotted quad ip address to a
string of a long int.

struct.unpack('L',socket.inet_aton(ip))[0]

trouble is when I use this, I get

struct.error: unpack str size does not match format

I thought ip addresses were unsigned 32 bit integers.

Is there a better way to take a dotted quad and convert it to a string
representation of an long int?

Works for me:
import socket
import struct
ip = "127.0.0.1"
struct.unpack('L',socket.inet_aton(ip))[0]
2130706433L

I really wish it worked for me:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format

This is python packaged with Suse 9.3.
dir(struct) ['__doc__', '__file__', '__name__', 'calcsize', 'error', 'pack', 'unpack']
print struct.__file__
/usr/lib64/python2.4/lib-dynload/struct.so

could I have a broken python?
I played around with format size and here are some results. (they don't make
sense)
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):

So, ip1s really should be a long unsigned int.. but it doesn't decode as
that. Is this the way to interpret this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top