Python示例-TCP Port Scan

 1 import socket
 2 import threading
 3 
 4 # target host address
 5 host = "127.0.0.1"
 6 # thread list
 7 threads = []
 8 
 9 
10 def tcp_connect_scan(host, port):
11     '''
12     TCP connect scanning
13     @param host: ip, host name or domain name
14     @param port: port number
15     '''
16     try:
17         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
18         sock.settimeout(1)
19         result = sock.connect_ex((host, port))
20         if result == 0:
21             print("Host {} port {} status open".format(host, port))
22         else:
23             print("Host {} port {} status close".format(host, port))
24     except socket.error as err:
25         print("{}".format(err))
26     finally:
27         sock.close()
28     
29 
30 def tcp_syn_scan(host, port):
31     '''
32     TCP SYN scanning
33     @param host: ip, host name or domain name
34     @param port: port number
35     '''
36     # To be continued
37     pass
38 
39 
40 def main():
41     for port in range(0, 65535):
42         t = threading.Thread(target=tcp_connect_scan, args=(host, port))
43         threads.append(t)
44         t.start()
45     for task in threads:
46         task.join()
47     
48 
49 if __name__ == '__main__':
50     main()
相關文章
相關標籤/搜索