You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
924B

  1. import socket
  2. # def get_local_ip():
  3. # client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  4. # # 连接谷歌的dns服务器
  5. # client.connect(("8.8.8.8", 80))
  6. # ip, _ = client.getsockname() # 获取套接字自己的地址,返回元组,ip地址和端口号
  7. # client.close()
  8. # return ip
  9. #
  10. #
  11. # if __name__ == '__main__':
  12. # print(get_local_ip())
  13. import socket
  14. from concurrent.futures import wait, ALL_COMPLETED, ThreadPoolExecutor
  15. def getport(port):
  16. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17. sock.settimeout(1)
  18. state = sock.connect_ex(("192.168.11.8", port))
  19. if 0 == state:
  20. print("port: {} is open".format(port))
  21. sock.close()
  22. with ThreadPoolExecutor(max_workers=100) as t:
  23. task_array = []
  24. for port in range(1, 65535):
  25. task = t.submit(getport, port)
  26. task_array.append(task)
  27. wait(task_array, return_when=ALL_COMPLETED)