如何检查Linux上的网络端口是否打开? [英] How to check if a network port is open on linux?

查看:143
本文介绍了如何检查Linux上的网络端口是否打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道使用python在Linux ubuntu而不是远程系统上是否打开/关闭了某个端口? 如何在python中列出这些打开的端口?

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: 有没有一种方法可以将netstat输出与python集成在一起?
  • Netstat: Is there a way to integrate netstat output with python?

推荐答案

您可以使用套接字模块,只需检查端口是否打开即可.

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

它看起来像这样.

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()

这篇关于如何检查Linux上的网络端口是否打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆