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

前端 未结 12 1037
半阙折子戏
半阙折子戏 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:27

    If you only care about the local machine, you can rely on the psutil package. You can either:

    1. Check all ports used by a specific pid:

      proc = psutil.Process(pid)
      print proc.connections()
      
    2. Check all ports used on the local machine:

      print psutil.net_connections()
      

    It works on Windows too.

    https://github.com/giampaolo/psutil

提交回复
热议问题