First Script Problem

Gustavo Picon gpicon at aureal.com.pe
Fri Sep 16 16:15:37 EDT 2005


On Fri, 2005-09-16 at 15:19 -0400, Ed Hotchkiss wrote:
> This script should just be writing every possibly IP (yea, there are
> billions i know blah blah) to a txt file. Instead of just writing the
> IP, it continues and the number goes past 4 groups. IE: The possible
> IP keeps getting 3 characters longer every time. And at the end of the
> last loops do I somehow have to set my mySet to NULL? Any ideas
> here?  

(SNIP)

> def findIPs():
>  for num in range(256):
>   mySet = "%03.d" % num
>   for num in range(256):
>    mySet = mySet + "." + "%03.d" % num
>    for num in range(256):
>     mySet = mySet + "." + "%03.d" % num 
>     for num in range(256):
>      mySet = mySet + "." + "%03.d" % num
>      ipFile.write(mySet+"\n")
> 
> findIPs()
> 
> ipFile.close()
> 

You should be using different variables in that algorithm, because you
are concatenating strings to "mySET" over and over again.

A more efficient approach would be something like:

def findIPs():
    ip = 0
    while ip < 256**4:
        print '.'.join('%03d' % (int(ip >> foo*8) & 255)\ 
            for foo in range(3, -1, -1))
        ip += 1



-- 
Gustavo Picon (http://tabo.aurealsys.com/)
Aureal Systems S.A.C. (http://www.aureal.com.pe/)
gpicon at aureal.com.pe
Tlf: 243-0131
Nextel: 9824*4625
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20050916/0401d2e3/attachment.sig>


More information about the Python-list mailing list