Steve Holden wrote:
Bob Smith wrote:

Does the Win32 port of Python have a memory leak? I have some code that runs flawlessly on Linux, but bombs after a few hours on Windows. It's threaded and uses a lot of memory.

Thanks!


Yes, that's a well-known problem. Code that runs with a few errors will port without any trouble at all to Windows, but once it runs flawlessly on Linux it starts to leak memory on Windows. The PSU suspects a plot in Redmond, the basic details of which ar

Oh, the humor of it all ;)

Attached is the code. Run it yourself and see. You too Peter. Be gentle with me, this was my first attempt with threads.
import os
import urllib
import socket
import time
import Queue
import threading

##########################################
#           Network Section              #
##########################################

socket.setdefaulttimeout(30)

networks = []
hosts = []
subnets = []

# Add the network 192.168.0 possibility to networks.
networks.append("192.168.0.")

# Generate and add networks 192.168.1-255 to networks.
n = 0
while n < 255:
   n = n + 1
   networks.append("192.168.%s." %(n))

# Generate and add hosts 1-254 to hosts.
for network in networks:
   h = 1
   # Add the n.n.n.0 host possibility to hosts.
   hosts.append(network+str(h))
   while h < 254:
      h = h + 1
      hosts.append(network+str(h))
      
# This should equal 65024 or 256 * 254
# because 256 possibilities are OK in the 3rd octet,
# but only 254 possibilities are OK in the 4th octet...
# we exclude 0 and 255.
print "There are", len(hosts), "total hosts (192.168.256*254) in the hosts 
list."

a = 0
b = 254

## Add the 192.168.0 net list to the subnets list.
subnets.append(hosts[0:254])
##print subnets
##print len(subnets)

## Add the 192.168.1-254 net lists to the subnets list.
for x in xrange(254):
    a = a+254
    b = b+254
    subnets.append(hosts[a:b])    
##print subnets
##print len(subnets)

## Add the 192.168.255 net list to the subnets list.
subnets.append(hosts[64770 :65024])
##print subnets[0]
print "There are", len(subnets), "class C network lists in the subnets list."
      
##########################################
#              Queue Section             #
##########################################

# Create a queue of urls to feed the threads
# Make it so that the queue only contains 256 items

nmap_queue = Queue.Queue(256)
for subnet in subnets:
    nmap_queue.put(subnet)

###########################################
#          Thread Section                 #
###########################################

class prac(threading.Thread):

    def run(self):
        net = nmap_queue.get()
        for ip in net:
            Y = os.popen('nmap -sT -p 80 -P0 -n %s' %ip)
            data = Y.read()
            Y.close()
            if 'open' in data:
                O = file('opened.txt', 'a')
                print >> O, ip
                O.close()
            elif 'closed' in data:
                C = file('closed.txt', 'a')
                print >> C, ip
                C.close()
            elif 'filtered' in data:
                F = file('filtered.txt' , 'a')
                print >> F, ip
                F.close()
            else:
                V = file('other.txt', 'a')
                print >> V, data, ip
                V.close()
        
threads = []
for i in xrange(256):
    go = prac()
    threads.append(go)
for thread in threads:
    thread.start()
while threading.activeCount() > 1:
    print str(threading.activeCount()), "threads running incl. main"
    time.sleep(1)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to