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

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:utf8
import socket
import sys
import multiprocessing
import time

def 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."


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值