螺纹端口扫描仪python

时间:2018-05-22 20:28:55

标签: multithreading python-2.7 network-programming

import socket
import threading
from Queue import *

print_lock = threading.Lock()
serverIp = socket.gethostbyname('www.google.com')

def pscan(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((serverIp, port))
        with print_lock:
            print('Port %d is open' %(port))
        s.close()
    except:
        pass

def threader():
    while True:
        worker = q.get()
        pscan(worker)
        q.task_done()

q = Queue()

for x in range(30):
    t = threading.Thread(serverIp = threading)
    t.daemon = True
    t.start()

for worker in range(1, 101):
    q.put(worker)

q.join()

我正在尝试在PyCharm中连接我的端口扫描程序但是我一直收到的错误是serverIp是行t = threading.Thread(serverIp = threading)上的意外关键字参数

这是什么亵渎?

1 个答案:

答案 0 :(得分:0)

您试图使其变得过于复杂。这是一个更清晰的版本,可在Pycharm中使用。 (归功于JackalDev)

import socket
import threading
import concurrent.futures
import colorama
from colorama import Fore

colorama.init()

print_lock = threading.Lock()

ip = input(Fore.BLUE + "Enter the IP to scan: ")

def scan(ip, port):
    scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    scanner.settimeout(1)
    try:
        scanner.connect((ip, port))
        scanner.close()
        with print_lock:
            print(Fore.BLUE + f"[{port}]" + Fore.GREEN + " Opened")
    except:
        pass

with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
    for port in range(1000):
        executor.submit(scan, ip, port + 1)