我正在学习python。这是一个脚本,我写了检查互联网连接即使连接了互联网,Python也会返回False
import os
import urllib2
from time import sleep
REMOTE_SERVER = "www.google.co.uk"
def is_connected():
try:
# see if we can resolve the host name -- tells us if there is
# a DNS listening
host = socket.gethostbyname(REMOTE_SERVER)
# connect to the host -- tells us if the host is actually
# reachable
s = socket.create_connection((host, 80), 2)
return True
except:
pass
return False
while(1):
if is_connected() == False:
print is_connected()
sleep(10)
的问题是,这个脚本返回即使我连接到互联网虚假。我可以ping www.google.co.uk但这个脚本只是返回false。有任何想法吗???
在'除''块'印刷'异常,而不是'通过'它 –
这不是一个很好的测试,即使它不太可能发生,如果谷歌失败怎么办 – meda