Use Python to detect whether a certain port of the server is successfully opened

Sometimes when configuring the server, when we think that a certain port has been opened, the result is often not like that. So we need a detection tool to detect whether some ports can be successfully accessed remotely. This function can be achieved with some port scanners. But we can do it here with a few simple lines of code using python.

import socket
def TelnetPort(server_ip,port):
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.settimeout(1) #设置超时时间
    try:
      sk.connect((server_ip,port))
      print('OK!')
    except Exception:
      print('Telnet Failed')
    sk.close()

server_ip='**********'
port=8000

TelnetPort(server_ip,port)

Through the function we wrote, we can perform detection at will, and we can also write a loop to scan whether a certain range of ports is successfully opened.

Guess you like

Origin blog.csdn.net/qq_40596572/article/details/105969752