tags:

views:

104

answers:

3

Hi , i was trying to make a full lot of ips for testing using this code :

ip_is = [i for i in range(256)]
ports = [i for i in range(1024,49152)]
return [str(i1)+"."+str(i2)+"."+str(i3)+"."+str(i4)+":"+str(p) for i1,i2,i3,i4,port in ip_is,ip_is,ip_is,ip_is,ports]

The problem is the 3rd line in which is made the ip list. If there is a way to make it all at once or how can make one at time in a lazy way ? I'm pretty noob at python :P.

Thanks for the Help :)

+4  A: 

You should use a generator instead of creating the full list:

def all_addresses():
   ip_is = [i for i in range(256)]
   ports = [i for i in range(1024,49152)]
   # note (...) instead of [...] to create a generator instead of a list;
   # separate |for|s to iterate over the lists individually
   return (str(i1)+"."+str(i2)+"."+str(i3)+"."+str(i4)+":"+str(p)
            for i1 in ip_is
            for i2 in ip_is
            for i3 in ip_is
            for i4 in ip_is
            for p in ports)

for addr in all_addresses():
   print addr

This way you will not run out of memory, but it will still take a very, very long time to iterate through all these addresses.

sth
Also, try '%d.%d.%d.%d:%d' % (i1,i2,i3,i4,p) for a slightly neater expression.
Jouni K. Seppänen
In Python 2, `[i for i in range(256)]` can be written simply as `range(256)`. In Python 3, just write `list(range(256))`.
Paul McGuire
This was the one i use it , and i restricted it to an amount of ips , because it would end in 2 years -.- ( and for that time it would be better to restart the process in a quantum pc :P ). Thanks a lot!
DraskyVanderhoff
+5  A: 
return ('%d.%d.%d.%d:%d' % (i1, i2, i3, i4, port) for i1, i2, i3, i4, port in itertools.product(ip_is, ip_is, ip_is, ip_is, ports))
Ignacio Vazquez-Abrams
It was what i want but for my pc was the most hardcore DOS ever. Even the mouse died. Thanks a lot anyway ;)
DraskyVanderhoff
Then you goofed up something else. This code won't do that.
Ignacio Vazquez-Abrams
+6  A: 

You're trying (quite apart from the syntax issues) to make a list of

256 * 256 * 256 * 256 * (49152 - 1024)

strings -- i.e., 206708186021888 strings... about two hundred thousand billions of strings.

If you made one per microsecond, that would take you 6.5 years (even quite apart from the problem of finding the petabytes of RAM to hold them).

I know you want to "make a full lot of ips for testing", but that's way too full a lot.

Why not take a random sample from this huge set, instead? E.g.:

import random

def random_address():
  ip = tuple(random.randrange(256) for i in range(4))
  port = random.randrange(1024, 49152)
  format = '.'.join(['%s'] * 4) + ':%s'
  return format % (ip + (port,))

now, if you want (e.g.) a million such strings for your testing, just do:

millionstrings = [random_address() for i in xrange(1000*1000)]
Alex Martelli
Useful answer. BTW, I got an error when I ran the code, so I added some parens: `return format % (ip + (port,))`. I'm assuming it's a precedence issue -- the `%` binding more tightly than the `+`? But I'm a Python beginner, so perhaps I'm confused.
FM
@FM, no, you're right, thanks -- +1 and I'm editing the A to fix the lack of parentheses!-)
Alex Martelli
You are right man , it was very dump from my part to try that. I use it the random string generator , something very similar of what you do but i want to give it a try and find a lazy way. Thanks for the advice , i'll be more respectful to my 4Gigs :P
DraskyVanderhoff