测试Internet连接 [英] Testing for Internet Connection
问题描述
你好互联网。
我想知道,有没有一种简单的方法来测试互联网连接?如果
没有,那么困难的方法是:p
-
在上下文中查看此消息: http://www.nabble.com/Testing-for-In...p18460572。 html
从Nabble.com的Python-python-list邮件列表存档发送。
Hello internet.
I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p
--
View this message in context: http://www.nabble.com/Testing-for-In...p18460572.html
Sent from the Python - python-list mailing list archive at Nabble.com.
推荐答案
Alexnb写道:
Alexnb wrote:
我想知道,有没有一种简单的方法来测试互联网连接?如果
没有,那么困难的方法是:p
I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p
试图从几个主要网站(雅虎,谷歌,
等)?如果所有这些都失败了,那么很可能连接
已经关闭。您可以使用urllib2 [1]来实现这一目标。
[1]< http://docs.python.org/lib/module-urllib2.html>
Trying to fetch the homepage from a few major websites (Yahoo, Google,
etc.)? If all of them are failing, it''s very likely that the connection
is down. You can use urllib2 [1] to accomplish that.
[1] <http://docs.python.org/lib/module-urllib2.html>
Alexnb< al ******** @ gmail.comwrites:
Alexnb <al********@gmail.comwrites:
我想知道,有没有一个简单的方法来测试互联网
连接?如果没有,那么困难的方法是:p
I am wondering, is there a simple way to test for Internet
connection? If not, what is the hard way :p
优化问题:互联网是什么意思?它不是单个
实体。
你的意思是某个特定的互联网主机响应特定的
网络端口?
如果您可以通过互联网连接确切地定义您的意思,那么
测试就相应地变得容易了。
-
\ a ??我为什么要关心子孙后代?什么后代做了什么|
` \对我来说? a ?? Groucho Marx |
_o__)|
Ben Finney
Refine the question: What do you mean by "internet"? It isn''t a single
entity.
Do you mean "some particular internet host responding on a particular
network port"?
If you can define exactly what you mean by "internet connection", the
test for it becomes correspondingly easier.
--
\ a??Why should I care about posterity? What''s posterity ever done |
`\ for me?a?? a??Groucho Marx |
_o__) |
Ben Finney
Alex Marandon写道:
Alex Marandon wrote:
Alexnb写道:
Alexnb wrote:
>我想知道,有没有一种简单的方法来测试互联网连接?如果没有,有什么困难:p
>I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p
试图从几个主要网站(雅虎,谷歌,
$)获取主页b $ b等)?如果所有这些都失败了,那么很可能连接
已经关闭。您可以使用urllib2 [1]来实现这一目标。
[1]< http://docs.python.org/lib/module-urllib2.html>
Trying to fetch the homepage from a few major websites (Yahoo, Google,
etc.)? If all of them are failing, it''s very likely that the connection
is down. You can use urllib2 [1] to accomplish that.
[1] <http://docs.python.org/lib/module-urllib2.html>
这似乎很有效,并且没有带宽浪费:
========= ========================================= ========= ===================
#!/ usr / bin / python
导入套接字, struct
def check_host(主机,端口,超时= 1):
"""
检查连接某个主人。
"""
#假设我们没有路线。
ret = False
#连接到主机。
尝试:
#create socket。
sock = socket.socket()
#create timeval结构。
timeval = struct.pack(" 2I",timeout,0)
#set socket timeout options。
sock.setsockopt(socket.SOL_SOCKET,socket.SO_RCVTIMEO,timeval)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_SNDTIMEO,timeval)
#连接到主机。
sock.connect((主机,端口))
#中止通信。
sock.shutdown(SHUT_RDWR)
#毕竟我们有连接。
ret = True
除了:
通过
#尝试在任何情况下关闭套接字。
尝试:
sock.close()
除外:
通过
返回
#----- --------------------------- main ---------------------- -----------
if check_host(" www.heise.de",80):
print" Horray !"
else:
print"我们已经失去了总部!"
========= ========================================= ========= ===================
我希望代码没问题,但总有一些东西可以做得更好。
评论? :)
干杯,
托马斯。
This seems to work and is rather fast and wastes no bandwidth:
================================================== ============================
#!/usr/bin/python
import socket, struct
def check_host(host, port, timeout=1):
"""
Check for connectivity to a certain host.
"""
# assume we have no route.
ret=False
# connect to host.
try:
# create socket.
sock=socket.socket()
# create timeval structure.
timeval=struct.pack("2I", timeout, 0)
# set socket timeout options.
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDTIMEO, timeval)
# connect to host.
sock.connect((host, port))
# abort communications.
sock.shutdown(SHUT_RDWR)
# we have connectivity after all.
ret=True
except:
pass
# try to close socket in any case.
try:
sock.close()
except:
pass
return ret
# -------------------------------- main ---------------------------------
if check_host("www.heise.de", 80):
print "Horray!"
else:
print "We''ve lost headquarters!"
================================================== ============================
I hope the code is ok, but there is always something you can do better.
Comments? :)
Cheers,
Thomas.
这篇关于测试Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!