我正在努力学习蟒蛇。这是我写的检查互联网连接的脚本
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)
问题是,即使我连接到Internet,此脚本也会返回false。我可以ping www. GoGoel.C.U.K.但是这个脚本只是返回false。有什么想法吗????