Python's way of detecting whether a remote port is open

  • 2020-04-02 14:42:20
  • OfStack

This article illustrates python's method for determining whether a remote port is open. Share with you for your reference. The specific implementation method is as follows:


import socket
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1)
try:
  sk.connect(('www.sharejs.com',80))
  print 'Server port 80 OK!'
except Exception:
  print 'Server port 80 not connect!'
sk.close()

I hope this article has helped you with your Python programming.


Related articles: