def socket_port(ip, PORT): global portdetaildict s = socket.socket() s.settimeout(0.1) if s.connect_ex((ip, PORT)) == 0: print ip, PORT, 'is open' portdetaildict[str(ip)] += str(PORT)+';' s.close()
def scan(ip): s = socket.socket() s.settimeout(0.1) common_port_list = [80, 443, 8080]#这里可以扩展成 200 个常用端口 for port in common_port_list: thread.start_new_thread(socket_port, (ip, int(port)))
def worker(q): while not q.empty(): ip = q.get() try: scan(ip) finally: q.task_done()
if __name__ == '__main__': q = multiprocessing.JoinableQueue() iplines = linecache.getlines('../test.iplist') timestart = time.time() for oneip in iplines: print oneip thisip = str(oneip).strip() map(q.put, thisip)
jobs = [multiprocessing.Process(target=worker, args=(q,)) for i in xrange(100)] map(lambda x:x.start(),jobs)
#想写个能快速扫完指定 iplist 里所有 ip 的指定若干端口是否开放的多线程 py ,我知道倒数第二行有问题不太知道如何把 ip 传进去。。 help 。。。我真是小白。。
@Allianzcortex 额我只想用一个 JoinableQueue 来解决问题,我这个代码现在唯一的问题就是 jobs = [multiprocessing.Process(target=worker, args=(q,)) for i in xrange(100)] 这句里 q 的赋值我没太搞懂,我想把 iplist 赋值进去好让它一个一个处理,对 python 语法不熟。。。求点拨!