By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Ip format

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.