How to check if a network port is open on linux?

前端 未结 12 1034
半阙折子戏
半阙折子戏 2020-12-07 11:07

How can I know if a certain port is open/closed on linux ubuntu, not a remote system, using python? How can I list these open ports in python?

  • Netstat: Is th
12条回答
  •  长情又很酷
    2020-12-07 11:33

    You can using the socket module to simply check if a port is open or not.

    It would look something like this.

    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('127.0.0.1',80))
    if result == 0:
       print "Port is open"
    else:
       print "Port is not open"
    sock.close()
    

提交回复
热议问题