HTTPConnection.request是否不遵守超时? [英] HTTPConnection.request not respecting timeout?

查看:114
本文介绍了HTTPConnection.request是否不遵守超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用HTTPConnection(2.7.8)进行请求,并且已将HTTPConnection(host, timeout=10)的超时设置为10.但是,HTTPConnection.request()似乎在10秒后不会超时.实际上,HTTPConnection.timeout似乎甚至没有被HTTPConnection.request()读取(它只能由HTTPConnection.connect()读取.我的理解正确吗?timeout仅适用于connect()而不是request()吗? request()超时的方法?

I'm trying to use HTTPConnection (2.7.8) to make a request and I've set the timeout to 10 with HTTPConnection(host, timeout=10). However, HTTPConnection.request() doesn't seem to timeout after 10 seconds. In fact, HTTPConnection.timeout doesn't even seem to be read by HTTPConnection.request() (it's only read by HTTPConnection.connect(). Is my understanding correct? Is timeout only applicable to connect() and not request()? Is there a way to timeout request()?

更新:

我认为我已经进一步缩小了问题的范围:如果我不提供该方案,它将不会遵守套接字超时.如果提供了方案,即完整URL为http://google.com:22222,则相应超时.我不知道为何该计划应能有所作为.也就是说,以下内容不考虑超时

I think I've narrowed the issue down further: if I don't provide the scheme, it won't respect the socket timeout. If the scheme was provided, i.e. the full URL being http://google.com:22222, then it'd time out accordingly. I wonder why the presence of the scheme should make a difference. That is, the following doesn't respect the timeout

    socket.setdefaulttimeout(3)
    conn = HTTPConnection('google.com:22222')
    conn.timeout = 3
    conn.request('GET', '')

而这样做:

    socket.setdefaulttimeout(3)
    conn = HTTPConnection('http://google.com:22222')
    conn.timeout = 3
    conn.request('GET', '')

但是,并非所有域都如此.

However, it doesn't happen to all domains.

谢谢

推荐答案

以下代码失败大约需要30秒:

It takes around ~30 seconds for the following code to fail:

#!/usr/bin/env python2
from httplib import HTTPConnection

conn = HTTPConnection('google.com', 22222, timeout=2)
conn.request('GET', '')

如果将IP传递给HTTPConnection而不是主机名,则超时错误将按预期在2秒钟内出现:

If ip is passed to HTTPConnection instead of the hostname then the timeout error is raised in 2 seconds as expected:

#!/usr/bin/env python2
import socket
from httplib import HTTPConnection

host, port = 'google.com', 22222
ip, port = socket.getaddrinfo(host, port)[0][-1]
conn = HTTPConnection(ip, port, timeout=2)
conn.request('GET', '')

解释与 ftplib中的解释相同.FTP超时行为不一致:超时可能会限制单个套接字操作,但它没有说明HTTPConnection()调用本身的持续时间,该持续时间可能会尝试getaddrinfo()返回的多个ip地址,并且超时仅限制单个套接字操作.合并多个操作可能需要更长的时间.

The explanation is the same as in ftplib.FTP timeout has inconsistent behaviour: the timeout may limit individual socket operations but it says nothing about the duration of the HTTPConnection() call itself that may try several ip addresses returned by getaddrinfo() and the timeout limits only the individual socket operations. Several operations combined may take longer.

您的HTTPConnection('http://google.com:22222')不久就会失败,因为url是一个错误的参数:它应该是hosthost:port.绝对urlrequest()方法接受-尽管即使它具有特殊含义-通常,您只需提供诸如'/'的路径即可.

Your HTTPConnection('http://google.com:22222') fails sooner because the url is an incorrect argument: it should be either host or host:port. The absolute url is accepted by request() method -- though even there it has special meaning -- typically, you just provide the path along such as '/'.

这篇关于HTTPConnection.request是否不遵守超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆