所以我使用了套接字 connect_ex,这意味着在 if 语句中,如果结果为真,则端口关闭,因为当它出现错误时它是真的,但如果它是假的,则端口是打开的,但它不工作。#!/usr/bin/python
import socket
ip = raw_input("Enter the IP address: ")
port =input("Enter The Port Number: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if sock.connect_ex((ip,port)):
print"Port" , port, "is closed"
else:
print "Port", port, "is closed"