python httplib 和断开的 TCP 连接

发布于 2024-08-31 15:20:00 字数 57 浏览 5 评论 0原文

如何使用 httplib 库查明连接是否已断开?看起来像是很基本的东西,但我在这里或谷歌找不到答案。

How do I find out if a connection has been broken using the httplib library? Seems like something so basic yet I can't find the answer on here or google.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2024-09-07 15:20:00

连接时您会得到以下之一:

http://docs.python.org/ Library/httplib.html#httplib.HTTPException

你可以做这样的事情。

>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> try:
>>>     conn.request("GET", "/index.html")
>>> except Exception as e:
>>>     #take action according to the error.
>>>     print(type(e))
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason

示例取自 www.python.org 并进行了编辑

While Connecting You get one of these:

http://docs.python.org/library/httplib.html#httplib.HTTPException

you could do something like this.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> try:
>>>     conn.request("GET", "/index.html")
>>> except Exception as e:
>>>     #take action according to the error.
>>>     print(type(e))
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason

Example taken from www.python.org and edited

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文