python扫描端口的例子

发布时间:2020-11-06编辑:脚本学堂
本文介绍了使用python实现扫描端口的例子,python实现端口扫描的方法,需要的朋友参考下。

例子,python最简洁易懂的扫描端口代码。
 

复制代码 代码示例:

#!/usr/bin/python
# www.jb200.com

from threading import Thread, activeCount
import socket
import os
def test_port(dst,port):
    os.system('title '+str(port))

    cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:

        indicator = cli_sock.connect_ex((dst, port))
        if indicator == 0:
            print(port)
        cli_sock.close()
    except:

        pass

if __name__=='__main__':
    dst = '192.168.0.22'

    i = 0
    while i < 65536:
        if activeCount() <= 200:
            Thread(target = test_port, args = (dst, i)).start()

            i = i + 1
    while True:
        if activeCount() == 2:

            break

    input('Finished scanning.')