找不到开放端口python

时间:2016-05-20 10:35:10

标签: python sockets python-multithreading

我正在尝试在python中创建一个简单的portscanner - 它试图查看端口0-10000是否打开,然后将它们附加到数组中。但是,即使我打开一个端口,它仍然无法识别端口是否打开。

由于

代码:

import socket
import threading

ip = "127.0.0.1"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #socket.AF_INET, socket.SOCK_STREAM
open_ports = []
def porttry(ip, port):
    try:
        s.connect((ip, port))
        print("Port opened on %d" % port)
        open_ports.append(port)
        return True
    except:

        return None

for port in range(0, 10000):
    threading.Thread(target=porttry,args=(ip,port)).start()
    if port%100==0:
        print(port) 

1 个答案:

答案 0 :(得分:1)

要使此代码起作用,您需要为每个连接(每个线程)单独创建套接字。

以下作品:

Custtable custTable;
ARandomTable mytable;
;
ttsBegin;
   select forUpdate custTable where custTable.AccountNum == '4000';
   custTable.NameAlias = custTable.Name;
   custTable.update();
   ttsBegin;
      select forUpdate mytable where mytable.myField == 'abc';
      mytable.myField = 'xyz';
      mytable.update();
      //ups something wrong happened... please abort the last
   ttsAbort;
ttsCommit;

它检测用例如import socket import threading ip = "127.0.0.1" open_ports = [] def porttry(ip, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, port)) print("Port opened on %d" % port) open_ports.append(port) s.close() return True except: return False for port in range(0, 10000): threading.Thread(target=porttry, args=(ip,port)).start() if port % 100 == 0: print(port) 打开的端口。