发布时间:2019-08-13 07:41:17编辑:auto阅读(2107)
关于socket.connect_ex和socket.connect, 前者是返回状态值而不是抛出异常. 适合这种判断可连接的情景.
Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call
当然, 复杂的判断还可以增加连接后和服务的交互, 使用send/sendall来发送数据以及recv/recvall来接收数据.
import socket
port_number = [80, 135, 445, 3306, 8080, 1080]
for index in port_number:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', index))
if result == 0:
print("Port %d is open" % index)
else:
print("Port %d is not open" % index)
sock.close()
另外, psutils也可以在这里使用,博文转载自网站.
上一篇: python MySQL 批量插入
下一篇: 十大排序算法总结(Python3实现)
50973
50356
40971
37839
32303
29194
28080
22910
22873
21205
1074°
1750°
1433°
1368°
1638°
1454°
2113°
3660°
3590°
2529°