没有所谓的捷径
一切都是时间最平凡的累积

python线程池ThreadPoolExecutor扫描端口

本文最后更新于2019年9月26日,已超过53天没有更新,如果文章内容失效,请反馈给我们,谢谢!

线程池: from concurrent.futures import ThreadPoolExecutor

网络服务:socket

#!/usr/local/python3.6.3/bin/python3.6
# coding = utf-8

import socket
import datetime
from concurrent.futures import ThreadPoolExecutor, wait

debug = False, 
timeout= 0.2
max_workers = 500


def get_ip(domain):
    try:
        ip = socket.gethostbyname(domain)
    except Exception as e:
        if debug:
            print("解析域名失败,请检查dns配置", e)
        raise e
    else:
        return ip


def portscan(ip, port, timeout):
    try:
        s = socket.socket()
        s.settimeout(timeout)
        s.connect((ip, port))
        openstr = f'[+] {ip} port:{port} open'
        print(openstr)
    except Exception:
        if debug == 1: 
            print(e) 
        else:
             pass
    finally:
        s.close


def main():
    debug, timeout, max_workers = get_setting()
    domain = input("请输入IP或域名:")
    ip = get_ip(domain, debug)
    start_time = datetime.datetime.now()
    executor = ThreadPoolExecutor(max_workers=max_workers)
    t = [executor.submit(portscan, ip, n, timeout) for n in range(1, 65536)]
    if wait(t, return_when='ALL_COMPLETED'):
        end_time = datetime.datetime.now()
        print("扫描完成,用时:", (end_time - start_time).seconds)
    input("按任意键退出")


if __name__ == '__main__':
    main()

 

赞(0) 打赏
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除,联系邮箱:lwarm@qq.com。文章观点不代表本站立场。本站原创内容未经允许不得转载,或转载时需注明出处:红岩子 » python线程池ThreadPoolExecutor扫描端口
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

今天所做的努力都是在为明天积蓄力量

联系我们赞助我们