473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4810
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.