2014-10-03 28 views
6

所以我做Python中的端口扫描器...的Python - 制作一个快速端口扫描器

import socket 
ip = "External IP" 
s = socket.socket(2, 1) #socket.AF_INET, socket.SOCK_STREAM 

def porttry(ip, port): 
    try: 
     s.connect((ip, port)) 
     return True 
    except: 
     return None 

for port in range(0, 10000): 
    value = porttry(ip, port) 
    if value == None: 
     print("Port not opened on %d" % port) 
    else: 
     print("Port opened on %d" % port) 
     break 
raw_input() 

但这是太慢了,我想以某种方式能够在一定的接近或突破后的代码一段时间没有返回任何东西。如果这是不可能的,请帮我无论哪种方式

回答

11

除了设置套接字超时,你还可以利用多线程技术涡轮推动这一进程。当您有N个端口进行扫描时,它的速度至多会快N倍。

# This script runs on Python 3 
import socket, threading 


def TCP_connect(ip, port_number, delay, output): 
    TCPsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    TCPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    TCPsock.settimeout(delay) 
    try: 
     TCPsock.connect((ip, port_number)) 
     output[port_number] = 'Listening' 
    except: 
     output[port_number] = '' 



def scan_ports(host_ip, delay): 

    threads = []  # To run TCP_connect concurrently 
    output = {}   # For printing purposes 

    # Spawning threads to scan ports 
    for i in range(10000): 
     t = threading.Thread(target=TCP_connect, args=(host_ip, i, delay, output)) 
     threads.append(t) 

    # Starting threads 
    for i in range(10000): 
     threads[i].start() 

    # Locking the script until all threads complete 
    for i in range(10000): 
     threads[i].join() 

    # Printing listening ports from small to large 
    for i in range(10000): 
     if output[i] == 'Listening': 
      print(str(i) + ': ' + output[i]) 



def main(): 
    host_ip = input("Enter host IP: ") 
    delay = int(input("How many seconds the socket is going to wait until timeout: ")) 
    scan_ports(host_ip, delay) 

if __name__ == "__main__": 
    main() 
2

考虑使用socket.setdefaulttimeout(timeout) for循环设置超时,而不是一个。

0

socket.setdefaulttimeout(0.5) 这将使程序更快!

+0

这个答案是一个好的开始,但我想解释为什么它会加快程序,或许与一些参考这将是有益的。 – Shepmaster 2014-12-27 03:10:42

0

socket.setdefualttimeout(时间)

来不断尝试与端口perticular同时连接...当你发送请求,并有超时设置2秒,所以它会尝试与端口连接2秒....如果将有来自该端口在2秒钟内没有响应....它将被算作一个死端口

0

这应该快一点。

#-*-coding:utf8;-*- 
#qpy:3 
#qpy:console 

import socket 
import os 

# This is used to set a default timeout on socket 
# objects. 
DEFAULT_TIMEOUT = 0.5 

# This is used for checking if a call to socket.connect_ex 
# was successful. 
SUCCESS = 0 

def check_port(*host_port, timeout=DEFAULT_TIMEOUT): 
    ''' Try to connect to a specified host on a specified port. 
    If the connection takes longer then the TIMEOUT we set we assume 
    the host is down. If the connection is a success we can safely assume 
    the host is up and listing on port x. If the connection fails for any 
    other reason we assume the host is down and the port is closed.''' 

    # Create and configure the socket. 
    sock = socket.socket() 
    sock.settimeout(timeout) 

    # the SO_REUSEADDR flag tells the kernel to reuse a local 
    # socket in TIME_WAIT state, without waiting for its natural 
    # timeout to expire. 
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 

    # Like connect(address), but return an error indicator instead 
    # of raising an exception for errors returned by the C-level connect()  
    # call (other problems, such as “host not found,” can still raise exceptions). 
    # The error indicator is 0 if the operation succeeded, otherwise the value of 
    # the errnovariable. This is useful to support, for example, asynchronous connects. 
    connected = sock.connect_ex(host_port) is SUCCESS 

    # Mark the socket closed. 
    # The underlying system resource (e.g. a file descriptor) 
    # is also closed when all file objects from makefile() are closed. 
    # Once that happens, all future operations on the socket object will fail. 
    # The remote end will receive no more data (after queued data is flushed). 
    sock.close() 

    # return True if port is open or False if port is closed. 
    return connected 


con = check_port('www.google.com', 83) 
print(con)