Core Python Programming . . .
FireNWater
khoard at gmail.com
Wed Jan 23 12:17:52 EST 2008
On Jan 22, 5:00 pm, wesley chun <wes... at gmail.com> wrote:
> > > 6-11 Conversion.
> > > (a) Create a program that will convert from an integer to an
> > > Internet Protocol (IP) address in the four-octet format of WWW.XXX.YYY.ZZZ
> > > (b) Update your program to be able to do the vice verse of the above.
>
> > I think it's is asking to convert a 32-bit int to the dotted form.
>
> > It's a little known fact, but IP addresses are valid in non-dotted
> > long-int form. Spammers commonly use this trick to disguise their IP
> > addresses in emails from scanners.
>
> that is correct. don't read too much into it. i'm not trying to
> validate anything or any format, use old or new technology. it is
> simply to exercise your skills with numbers (specifically 32-bit/4-
> byte integers), string manipulation, and bitwise operations. if you
> wish to use different sizes of numbers, forms of addressing, IPv6,
> etc., that's up to you. don't forget about part (b), which is to take
> an IP address and turn it into a 32-bit integer.
>
> enjoy!
> -- wesley
>
> ps. since you're on p. 248, there is also a typo in the piece of code
> right above this exercise, Example 6.4, which is tied to exercise
> 6-7. "'fac_list'" should really be "`fac_list`", or even better,
> "repr(fac_list)". see the Errata at the book's websitehttp://corepython.com
> for more details.
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com
Well, I think I may be way off on this one. . . here's what I came up
with. . . .
def int_to_IP(int):
'''Function accepts an integer and returns a string in the format
WWW.XXX.YYY.ZZZ'''
string = str(int)
return (string[0]*3 + '.' + string[1]*3 + '.' + string[2]*3 + '.'
+ string[3]*3)
number = int(raw_input('Enter the number(4 digits): '))
print (int_to_IP(number))
More information about the Python-list
mailing list