473,396 Members | 2,004 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,396 software developers and data experts.

Ip format

Hello i have this ip 1578568204

how socket function i can have the ip dotted-quad string?

i have try socket.inet_aton get no error but only this ^ETB FF

Thanks luca

Nov 20 '08 #1
2 2599
Hello i have this ip 1578568204
>
how socket function i can have the ip dotted-quad string?

i have try socket.inet_aton get no error but only this ^ETB FF

I've got the following program I threw together for one of my
junior developers:

from sys import argv, exit
if len(argv) <2:
print "Usage:"
print "\t%s <ip_address>" % argv[0]
print "\t%s <int>" % argv[0]
print "\nThe first form translates a dotted-quad format into
int format"
print "\nThe second form translates from int form to a
dotted-quad"
exit(1)

src = argv[1]

if src.count('.') == 3:
a,b,c,d = map(lambda s: 0xff and int(s), src.split('.'))
print (
(a << (8*3)) |
(b << (8*2)) |
(c << (8*1)) |
d)
else:
i = int(src)
a = 0xff & (i >(8*3))
b = 0xff & (i >(8*2))
c = 0xff & (i >(8*1))
d = 0xff & (i)
print '%s.%s.%s.%s' % (a,b,c,d)

The code you're looking for is in the bottom "else:" clause.
Yes, that could be optimized to just "c = 0xff & (i >8)" in the
3rd case, but it made it easier for my junior developer to
understand what was going on.

There might be some library function to do these transformations
for you, but after a quick look, I didn't find anything.

-tkc


Nov 20 '08 #2
Many thanks for your help
I have also find the correct socket function:
ip 1578568204
ip = socket.inet_aton(ip)
ip_dot = socket.inet_ntoa(ip)
Thanks Luca
On 20 Nov, 12:36, Tim Chase <python.l...@tim.thechases.comwrote:
Hello i have this ip 1578568204
how socket function i can have the ip dotted-quad string?
i have try socket.inet_aton get no error but only this ^ETB FF

I've got the following program I threw together for one of my
junior developers:

* *from sys import argv, exit
* *if len(argv) <2:
* * *print "Usage:"
* * *print "\t%s <ip_address>" % argv[0]
* * *print "\t%s <int>" % argv[0]
* * *print "\nThe first form translates a dotted-quad format into
int format"
* * *print "\nThe second form translates from int form to a
dotted-quad"
* * *exit(1)

* *src = argv[1]

* *if src.count('.') == 3:
* * *a,b,c,d = map(lambda s: 0xff and int(s), src.split('.'))
* * *print (
* * * *(a << (8*3)) |
* * * *(b << (8*2)) |
* * * *(c << (8*1)) |
* * * *d)
* *else:
* * *i = int(src)
* * *a = 0xff & (i >(8*3))
* * *b = 0xff & (i >(8*2))
* * *c = 0xff & (i >(8*1))
* * *d = 0xff & (i)
* * *print '%s.%s.%s.%s' % (a,b,c,d)

The code you're looking for is in the bottom "else:" clause.
Yes, that could be optimized to just "c = 0xff & (i >8)" in the
3rd case, but it made it easier for my junior developer to
understand what was going on.

There might be some library function to do these transformations
for you, but after a quick look, I didn't find anything.

-tkc
Nov 20 '08 #3

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

Similar topics

3
by: Lowell Kirsh | last post by:
In Peter Norvig's Infrequently Answered Questions he explains that the following 2 fnctions look almost identical but are not the same: def printf(format, *args): print format % args, def...
15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
3
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
6
by: Dario Di Bella | last post by:
Hi all, we have the following urgent issue affecting our development team. Initially we had one particular workstation that failed executing queries on a DB2 database, raising an invalid date...
3
by: Melissa | last post by:
What specifically causes the Format event of a report's section to fire? Thanks! Melissa
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
4
by: David Morris | last post by:
Hi Could somebody please explain what the following line of code means String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1 It's actually the first argument that I don't understand. What is...
13
by: Roy | last post by:
Hi all, I'm creating a project that should always use this date format when displays the dates or create dates. The back end database is a SQL Server and I like to know what is the logical way...
3
by: Carl Trachte | last post by:
Hello. Python 3.0.a1 has been released. I'm trying to get the hang of the new string formatting in the form: '5.66' There are more options in PEP 3101 (fill, alignment, etc.), but I'm having...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
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
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...
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,...

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.