35 lines
924 B
Python
35 lines
924 B
Python
import socket
|
|
|
|
# def get_local_ip():
|
|
# client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
# # 连接谷歌的dns服务器
|
|
# client.connect(("8.8.8.8", 80))
|
|
# ip, _ = client.getsockname() # 获取套接字自己的地址,返回元组,ip地址和端口号
|
|
# client.close()
|
|
# return ip
|
|
#
|
|
#
|
|
# if __name__ == '__main__':
|
|
# print(get_local_ip())
|
|
|
|
|
|
import socket
|
|
from concurrent.futures import wait, ALL_COMPLETED, ThreadPoolExecutor
|
|
|
|
|
|
def getport(port):
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(1)
|
|
state = sock.connect_ex(("192.168.11.8", port))
|
|
if 0 == state:
|
|
print("port: {} is open".format(port))
|
|
sock.close()
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=100) as t:
|
|
task_array = []
|
|
for port in range(1, 65535):
|
|
task = t.submit(getport, port)
|
|
task_array.append(task)
|
|
wait(task_array, return_when=ALL_COMPLETED)
|