如何使用python检查特定端口的连接是否已打开

时间:2021-07-09 07:23:11

I need to check whether a connection with some port is running on windows. I can do it using netstat, but how can I check it using python?

我需要检查一些端口的连接是否在Windows上运行。我可以使用netstat来做,但是如何使用python进行检查?

1 个解决方案

#1


Try like this:

试试这样:

import socket;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = s.connect_ex(('192.1.1.1',80))
if res == 0:
   print "Open"
else:
   print "Closed"

#1


Try like this:

试试这样:

import socket;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = s.connect_ex(('192.1.1.1',80))
if res == 0:
   print "Open"
else:
   print "Closed"