【问题标题】:Issues with Python 3 Multiprocessing Pool Port ScannerPython 3 多处理池端口扫描程序的问题
【发布时间】:2018-04-02 23:30:21
【问题描述】:

代码是:

from multiprocessing import Pool
import socket

target = 'hackthissite.org'
list1 = []
def f(port):
    s = socket.socket()
    try:
        con = s.connect((target,port)
        print("Success:",port)
        s.close()
    except:
        print("Didn't work:",port)
        s.close()
        pass

#This function creates a list to iterate through for the map function
def port_list():
        port_range = int(input("How many ports would you like to scan?"))
        for x in range(1, port_range + 1):
             list1.append(x)

if __name__ == '__main__':
     port_list()
     with Pool(2) as p:
            p.map(f,list1)
            print("Done")

无论池中有多少端口或工人,它都会给我: 调试适配器进程意外终止

但是,如果我使用 p.map(f,(80,)) 手动传递我想要的端口,它就可以工作 100% 没问题。出于某种原因,传递给它一个相对较大的列表,例如 range(1,80) 并使用任意数量的进程杀死它?顺便说一句,这是在 VS Code 中。

【问题讨论】:

  • “调试适配器进程已意外终止”听起来 VS Code 的调试功能在子进程中遇到了问题。如果您在没有 VS Code 的调试器的情况下运行,您是否尝试过?

标签: python-3.x python-multiprocessing port-scanning


【解决方案1】:

您可能想要添加超时。

socket.setdefaulttimeout(.5)

不要吞下所有异常,而是使用

except socket.timeout:

【讨论】:

    【解决方案2】:

    试试这个:

    from multiprocessing import Pool
    import socket
    
    target = 'hackthissite.org'
    list1 = []
    def f(port):
        s = socket.socket()
        s.settimeout(.25)
        try:
            con = s.connect((target,port))
            print("Success:" + str(port))
            s.close()
        except:
            print("Didn't work:" + str(port))
            s.close()
            pass
    
    #This function creates a list to iterate through for the map function
    def port_list1():
            port_range = input("How many ports would you like to scan?").split(' ')
            for val in port_range:
                 list1.append(int(val))
    
            print(list1)
    
    def port_list2():
            port_range = input("enter the port range you want to scan ex 1-10 ").split('-')
            for i in range(int(port_range[0]), int(port_range[1]) + 1):
                 list1.append(int(i))
    
            print(list1)
    
    if __name__ == '__main__':
         port_list2()
         with Pool(2) as p:
                print(p.map(f,list1))
                print("Done")
    

    第一个函数让你手动输入端口,用空格隔开,所以试试22和80,应该可以成功。我在使用port_list1() 运行时使用22 80 8080 进行输入,然后使用port_list2() 进行1-1000 输入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多