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

Home Posts Topics Members FAQ

inet_aton and struct issue

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?

--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
Aug 30 '06 #1
3 4820
David Bear schrieb:
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
Aug 30 '06 #2
Diez B. Roggisch wrote:
David Bear schrieb:
>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:
>>struct.unpack('L', socket.inet_aton('129.219.120.129'))
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?
>
Diez
--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
Aug 30 '06 #3
David Bear wrote:
Diez B. Roggisch wrote:
>David Bear schrieb:
>>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:
>>>struct.unpack('L', socket.inet_aton('129.219.120.129'))
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?
>>
Diez
I played around with format size and here are some results. (they don't make
sense)
>>ip1 = '123.254.254.252'
import socket
import struct
ip1s = socket.inet_aton(ip1)
struct.unpack('L',ip1s)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format
>>struct.unpack('f',ip1s)
(-1.0592039033369304e+37,)
>>struct.unpack('I',ip1s)
(4244569723L,)
>>struct.unpack('Q',ip1s)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format
>>struct.unpack('L',ip1s)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format
>>struct.unpack('i',ip1s)
(-50397573,)
>>struct.unpack('I',ip1s)
(4244569723L,)
>>>
So, ip1s really should be a long unsigned int.. but it doesn't decode as
that. Is this the way to interpret this?

--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
Aug 30 '06 #4

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

Similar topics

10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
12
by: Sadeq | last post by:
Is there a way to read a struct field by field? I want to write a rather general function, like: public string EncodeStruct (struct strct) { .... } which accepts a general struct type,...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
2
by: BruceWho | last post by:
Hi, all I used inet_aton in my Visual C++ application, but got an error: error C2065: 'inet_aton' : undeclared identifier I searched the web and it seems that Visual C++ doesn't have...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
5
by: Alexander Adam | last post by:
Hi! I've got this issue: I have a struct like this: struct node { unsigned int m_code; ?? m_data; unsigned int m_data_size; };
5
by: karthikbalaguru | last post by:
Hi, struct sky { int stars; struct ocean *oceanptr; }; struct ocean { int waves; struct sky *skyptr;
6
by: akapsycho | last post by:
Is it INT(10)? I've done some reading and found out that INET_ATON() should make my MySQL queries fastest, but I'm not sure which data type to store it as. As far as I can tell, the maximum value...
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
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...
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,...
1
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: 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...
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 ...
0
muto222
php
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.