测试Python套接字模块的任何连接
有没有办法检查与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)
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"