Python线程新手-似乎没有什么不同 [英] New to Python Threading - It doesn't appear to make a difference

查看:91
本文介绍了Python线程新手-似乎没有什么不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将自动vnc扫描仪拼凑在一起.它在IP地址之间循环,并且如果检测到5900开放端口,则会尝试截屏.它不是很漂亮,构造很差,但是可以用.但是它很慢.我尝试过线程化进程,但一直在努力.您可以看到我已经添加了一个计时器,因此可以看到扫描30 ip需要多长时间.我已经尝试了多种类型的线程和线程库.当前的迭代可能是我拥有的最快的迭代,但是它仅比不使用线程快几秒钟.如果您能提供一些反馈意见,我将不胜感激.

I've cobbled together an automatic vnc scanner. It cycles through IP addresses and if it detects an open port 5900 it attempts a screen shot. It isn't pretty, and poorly constructed but it works. It is however slow. I've tried threading the processes but I've been struggling. You can see I've added in a timer so I can see how long it takes to scan 30 ip's. I've tried multiple types of threading and threading libraries. This current iteration is probably the fastest I've had it, but it is only a few seconds faster than without threading. I'd be grateful if you could provide some feedback.

非常感谢

    import socket
    import nmap
    from vncdotool import *
    from ipaddress import *
    import pexpect
    import time
    from multiprocessing import Pool, freeze_support
    from multiprocessing.dummy import Pool as ThreadPool
    import itertools



    def vncconnect(tgtHost):
        try:
            ip = str(tgtHost)
            command = 'vncdotool -v -s ' + ip + ' --delay=1000 capture %s' % (ip + '.jpg')
            child = pexpect.spawn(command)
            child.expect ('INFO:root:connecting')
            time.sleep (10)
            print 'attempting screenshot on ' + ip
            child.expect (pexpect.EOF)
        except:
            pass

    def nmapScan(tgtHost,tgtPort):

        try:
            nmScan = nmap.PortScanner()
            result = nmScan.scan(str(tgtHost),str(tgtPort))
            if (result['nmap']['scanstats']['uphosts'] == '1'):
            print 'Trying ' + tgtHost + ' - appears open: attempting to connect'
            vncconnect(tgtHost)
            f = open('database', 'r+')
                f.write(tgtHost + ' Banner: ' + result['scan']['190.81.24.103']['tcp'][5900]['name'] + result['scan']['190.81.24.103']['tcp'][5900] /               ['version'] + '/n')

            else:
            print 'Trying ' + tgtHost + ' - is not open'
        except:
            pass


    def main():
        net4 = IPv4Address(u'170.0.0.0')
        y = 0
        start = time.time()
        numberofhoststoscan = 30
        while y < numberofhoststoscan:
            try:            

                port = '5900'
                y = y + 1
                z = str(net4)
                nmapScan(z, port)
                net4 = net4 + 1

            except:
                pass            
                net4 = net4 + 1
        end = time.time()
        total = (end - start)   
        print 'total scan time = ' + str(total) + ', scanned ' + str(numberofhoststoscan) + ' hosts'

    if __name__ == "__main__":
        freeze_support()
        pool = ThreadPool(4)
        pool.map(main())
        pool.close() 
        pool.join()

推荐答案

它看起来像:

  1. 在启动线程池之前,请执行整个扫描:pool.map(main()).不要调用main,只需传递对象:pool.map(main)
  2. 每个线程将开始扫描同一组IP地址.您可能希望每个线程扫描一组不同的IP地址,以便在它们之间分配工作.
  1. You perform the entire scan before you start the thread pool: pool.map(main()). Don't call main, just pass the object: pool.map(main)
  2. Each thread will start scanning the same set of IP addresses. You would want each thread to scan a different set of IP addresses in order to divide the work between them.



更新:我将使用生成器生成地址扫描



Update: I would use a generator to produce the addresses to scan

def addressesToScan(firstAddress, numberofhoststoscan):
    net4 = IPv4Address(firstAddress)
    for y in range(numberofhoststoscan):
        yield net4
        net4 = net4 + 1

要使用它,您需要一个接受地址的函数.要测量总时间,您需要在线程的工作程序之外进行测量.

To use it, you need a function that accepts the address. To measure the total time, you need that measurement outside of the thread's worker.

def worker(targetHost):
    port = '5900'
    try:
        nmapScan(targetHost, port)
    except:
        pass

if __name__ == "__main__":
    freeze_support()
    pool = ThreadPool(4)
    start = time.time()
    pool.map(worker, addressesToScan(u'170.0.0.0', 30))
    pool.close()
    pool.join()

    end = time.time()
    total = (end - start)   
    print 'total scan time = ' + str(total) + ', scanned ' + str(numberofhoststoscan) + ' hosts'

这篇关于Python线程新手-似乎没有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆