测试任何连接 Python 套接字模块



有没有办法检查与 Python 套接字模块的连接?

while connection==True:

我正在寻找这样的东西,其中连接是模块的任何功能。

您可以像这样测试try块中是否存在连接:

import socket
def alive(host, port):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        translate = socket.gethostbyname(host)
        test = s.connect_ex((translate, port))
        return test
    except socket.error:
        return 1
test=alive(host,port)
if test ==0:
    print "success"
else:
    print "error"

最新更新