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

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

    Building upon the psutil solution mentioned by Joe (only works for checking local ports):

    import psutil
    1111 in [i.laddr.port for i in psutil.net_connections()]
    

    returns True if port 1111 currently used.

    psutil is not part of python stdlib, so you'd need to pip install psutil first. It also needs python headers to be available, so you need something like python-devel

提交回复
热议问题