欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用python实现扫描端口示例

程序员文章站 2023-04-07 21:25:41
python最简洁易懂的扫描端口代码.运行绝对会很有惊奇感 复制代码 代码如下:from threading import thread, activecount im...

python最简洁易懂的扫描端口代码.运行绝对会很有惊奇感

复制代码 代码如下:

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.')