Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试Python套接字模块的任何连接_Python - Fatal编程技术网

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

测试Python套接字模块的任何连接,python,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)

有没有办法检查与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"