Python端口扫描(多进程版+单线程版)

时间:2021-11-07 08:30:17

Python端口扫描单线程版:

#!/usr/bin/env python
#coding:utf8
import socket
ip = 'www.0x6f.info'
for port in range(1,1024,1):
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(2)
print "正在扫描端口: %d" % port
try:
sk.connect((ip,port))
print 'Server %s port %d OK' % (ip,port)
except Exception:
print 'Server %s port %d is not connected!' % (ip,port)
sk.close()


Python端口扫描多进程版:

#coding:utf8import socketimport sysimport multiprocessingimport timedef test(port):    ip = 'www.chlinux.net'    port = int(port)    print port    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    sk.settimeout(0.5)    print "正在扫描端口: %d" % port    try:        sk.connect((ip,port))        print "Server %s port %d OK" % (ip,port)    except Exception:        print "Server %s port %d is not connected!" % (ip,port)    sk.close()if __name__ == '__main__':    pool = multiprocessing.Pool(processes=20)    for port in xrange(1,65535):        port = int(port)        pool.apply_async(test,(port, ))    pool.close()    pool.join()    print "Sub-process(es) done."


更多博客尽在 Linux开源技术博客

本文出自 “平凡的日子” 博客,请务必保留此出处http://wolfchen.blog.51cto.com/2211749/1403366