python对端口进行扫描

使用cocket模块配合多线程对端口进行扫描,后续功能正在思考ing.

import socket
from multiprocessing.dummy import Pool as ThreadPool

def main():
    global host_ip
    host = input("Enter a host to scan:")
    startPort = int(input("Enter the start port:"))
    endPort = int(input("Enter the end port:"))
    processes_num = int(input("Enter processes num :"))
    host_ip = socket.gethostbyname(host)
    ports = []
    print('-' * 60)
    print('Please wait,scanning host',host_ip)
    print('-' * 60)
    socket.setdefaulttimeout(0.2)
    for port in range(startPort, endPort):
        ports.append(port)
    pool = ThreadPool(processes = processes_num)
    results = pool.map(get_ip_status,ports)
    pool.close()#关闭进程池,不在接收新的进程
    pool.join()#主线程阻塞等在子进程的退出
    print('port scan end')
def get_ip_status(port):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        server.connect((host_ip, port))
        print('[+] {0} port {1} is open'.format(host_ip, port))
    except Exception as err:
        pass
    finally:
        server.close()

if __name__ == '__main__':
    main()

 大佬们,这是我的菜鸟程序,有什么问题及好的建议,希望随时交流,联系方式:[email protected]

本站资源均源自网络,若涉及您的版权、知识产权或其他利益,请附上版权证明邮件告知。收到您的邮件后,我们将在72小时内删除。
若下载资源地址错误或链接跳转错误请联系站长。站长q:770044133。

» python对端口进行扫描

发表评论

免登录下载网,提供全网最优质的资源集合!