使用Python识别监听端口
在从 bash 翻译一些脚本时,我遇到了许多使用 netstat -an 来查找我们的服务之一是否正在侦听的情况。虽然我知道我可以只使用 subprocess.call 或其他甚至 popen 我宁愿使用 pythonic 解决方案,所以我没有利用我们正在运行的 unix 环境。
从我读到的内容来看,套接字模块应该有一些东西,但我还没有看到任何检查侦听端口的内容。可能是我不理解一个简单的技巧,但到目前为止我知道如何连接到套接字,并编写一些东西让我知道连接何时失败。但我不一定找到了专门检查端口以查看其是否正在监听的东西。
有什么想法吗?
In translating some scripts from bash, I am encountering many uses of netstat -an to find if one of our services is listening. While I know I can just use subprocess.call or other even popen I would rather use a pythonic solution so I am not leveraging the unix environment we are operating in.
From what I have read the socket module should have something but I haven't seen anything that checks for listening ports. It could be me not understanding a simple trick, but so far I know how to connect to a socket, and write something that lets me know when that connection failed. But not necessarily have I found something that specifically checks the port to see if its listening.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试连接怎么样...
How about trying to connect...
我知道这个问题很旧,但我是为初学者写的。如果您想识别系统上的侦听端口,可以使用下面的代码。
I know this question is old, but I write this for beginners. If you want to identify listening ports on your system, you can use the code below.
在 Linux 上,我们可以使用 strace 来查看 netstat -ln 正在读取并
解析 /proc 文件系统中的各种值。
所以你可以从Python读取这些文件并提取你想要的数据
需要。
侦听套接字将具有远程地址 00000000:0000
地址:端口对为十六进制。看:
* 我如何匹配每个/proc/net/tcp 条目到每个打开的套接字?
您可以与 /proc//fd 交叉引用。例如,sshd 是
在我的笔记本电脑上运行。
套接字 6456 对应于第二行列出的 inode 6458
/proc/net/tcp.conf
所以你可以从proc中获取所有这些信息,但你最终可能会
重新发明 netstat -lntp
On Linux we can use strace to see that netstat -ln is reading and
parsing various values from the /proc filesystem.
So you can just read those files from Python and extract the data you
need.
The listening sockets will have remote address 00000000:0000
The address:port pairs are in hex. See:
* How can i match each /proc/net/tcp entry to each opened socket?
You could cross reference with /proc//fd. For example, sshd is
running on my laptop.
Socket 6456 corresponds to inode 6458 listed in the second row of
/proc/net/tcp.
So you can get all this information from proc, but you may end up
reinventing netstat -lntp
参考:
https://stackoverflow.com/a/6244270
Reference:
https://stackoverflow.com/a/6244270
您可以尝试连接到有问题的端口,或模拟
netstat
。执行后者将是特定于操作系统的。在 Linux 上,您可以检查
/proc/net/tcp
。它看起来像这样:您正在查找
st
(“状态”)列中包含0A
的行。local_address
中冒号后面的数字 -C809
、16CC
等 - 是有侦听进程的 TCP 端口号(十六进制) 。You could either try connecting to the port in question, or emulate
netstat
.Doing the latter will be OS-specific. On Linux you can examine
/proc/net/tcp
. It looks like this:You're looking for lines that have
0A
in thest
("status") column. The numbers after the colon inlocal_address
--C809
,16CC
etc -- are TCP port numbers (in hex) on which there are listening processes.我知道我晚了几年,但现有的答案都不够好。
我在谷歌上为完全相同的问题寻找一个好的、优雅的解决方案,但已经发布的答案似乎都不够好,相反,我找到了自己的解决方案,我想将它们发布在这里,以帮助未来被重定向到这里的读者谷歌。
大多数操作系统都有一个名为
netstat
的可执行文件,可用于捕获侦听端口,在本例中我使用的是 Windows 10 和 Python 3.9.6 x64,但这是用 Python 编写的,因此您可以轻松适应它适合您自己的用例。使用纯
netstat
会非常慢,因为要进行所有名称解析,而使用netstat -n
会以指数方式更快,因为它不会浪费时间解析名称。在 Python 3.9.6 中,使用
subproces.run()
运行操作系统调用,并使用capture_output=True
捕获 stdout,然后使用.stdout
> 结果过程的属性来获取输出,结果是二进制形式,使用.decode()
获取字符串。那么输出应该是这样的:
使用 splitlines() 来获取单独的行,然后使用列表切片来获取实际表的内容,这里我们使用索引 4,然后使用正则表达式分割来分割空格字符,然后使用索引来获取本地地址值,最后使用冒号上的字符串分割和索引来获取端口。
代码:
或者简单地说:
性能:
或者,
psutil
有一个.net_connections()
方法,您可以从中获取端口。只需使用集合理解来获取输出:
这种方法比前一种方法要快得多:
I know I am several years late, but non of the existing answers are good enough.
I was Google searching for a good, elegant solution for exactly the same problem and non of the answers already posted seemed good enough, instead I have found solutions of my own and I want to post them here to help future readers who get redirected here by Google.
Most operating systems have an executable named
netstat
, that can be used to capture listening ports, in this example I am using Windows 10 and Python 3.9.6 x64, but this is written Python so you can easily adapt it for your own use case.Using plain
netstat
will be very slow, because of all the name-resolving, usingnetstat -n
will be exponentially faster because it doesn't waste time resolving the names.In Python 3.9.6, use
subproces.run()
to run os calls, and usecapture_output=True
to capture stdout, then use.stdout
property of resultant process to get output, the result is in binary form, use.decode()
to get string.Then the output should look like this:
Use
splitlines()
to get separate lines, then use list slicing to get the contents of the actual table, here we use index 4, then using regex splitting to split on whitespace characters, then use index to get the local address value, then finally use string split on colons and indexing to get the ports.The code:
Or in one-liner:
Performance:
Or, alternatively,
psutil
has a.net_connections()
method, you can just get the ports from it.Just use set comprehension to grab the output:
This approach is tremendously faster than the previous one: