【问题标题】:python, send many HTTP requests through one network connectionpython,通过一个网络连接发送多个HTTP请求
【发布时间】:2013-07-30 13:07:41
【问题描述】:

我正在编写一个 python 2.7 脚本,该脚本必须在 Fedora Commons 存储库中检查 20'000 个对象中是否存在某些数据。 基本上,这意味着将 20'000 个 HTTP 请求发送到存储库(在 Tomcat 服务器上运行)上的 20'000 个不同的 url。

我写了一个脚本来完成这项工作,但是服务器系统管理员警告我它打开了太多的网络连接,这会导致一些麻烦。

到目前为止,我的脚本使用 urllib2 来发出 HTTP 请求。

response         = urllib2.urlopen(url)
response_content = response.read()

实际上,这段代码会为每个请求打开一个新的网络连接。

我尝试使用其他库来发出请求,但找不到任何方法为所有请求重用相同的连接。下面的两种解决方案仍然打开许多网络连接,即使它们的数量非常少(实际上这两种解决方案似乎都为 100 个 HTTP 请求打开一个连接,在我的情况下仍然是大约 200 个连接)。

httplib:

url       = "http://localhost:8080/fedora/objects/test:1234?test="
url_infos = urlparse(url)
conn      = httplib.HTTPConnection(url_infos.hostname + ":" + str(url_infos.port))

for x in range(0, 20000):
    myurl = url + str(x)
    conn.request("GET", myurl)
    r = conn.getresponse()
    response_content = r.read()
    print x, "\t", myurl, "\t", r.status

请求:

url = "http://localhost:8080/fedora/objects/test:1234?test="
s   = requests.Session()

for x in range(0, 20000):       
    myurl = url + str(x)
    r = s.get(myurl)
    response_content = r.content
    print x, "\t", myurl, "\t", r.status_code

即使连接数要好得多,理想情况下,我希望为所有请求使用一个或很少的连接。这甚至可能吗?每个连接的 100 个请求数是与系统相关还是与服务器相关?顺便说一句,我也尝试让请求指向 Apache 服务器,结果是一样的。

【问题讨论】:

  • 我最好的猜测是这与服务器有关,而不是 Python 代码。您的两个解决方案都使用 httlib.HTTPConnection (请求在后台使用它),但是该类中的任何地方都没有幻数 100。我责怪服务器。
  • 您使用的是哪个版本的请求?来自当前版本的文档:thanks to urllib3, keep-alive is 100% automatic within a session!

标签: python-2.7 connection python-requests httplib httplib2


【解决方案1】:

两种解决方案共享一些代码,就像 Lukasa 所说的那样,而且在查询 Apache 或 Tomcat 时,两种结果都是等效的 让我首先想到它与 Python 代码有关。但其实和服务器配置有关。

诀窍在于 Apache 和 Tomcat 共享一个设置,该设置指示在同一个 TCP 连接中可以发出多少 HTTP 请求。两者的默认值都是 100。

雄猫:

maxKeepAliveRequests:

    The maximum number of HTTP requests which can be pipelined until the connection is closed by the server.
    If not specified, this attribute is set to 100.

http://tomcat.apache.org/tomcat-7.0-doc/config/http.html#Standard_Implementation

阿帕奇:

MaxKeepAliveRequests:

    The MaxKeepAliveRequests directive limits the number of requests allowed per connection when KeepAlive is on
    Default:    MaxKeepAliveRequests 100

http://httpd.apache.org/docs/2.2/en/mod/core.html#maxkeepaliverequests

通过修改这些值,实际上只能创建很少的连接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2022-07-26
    • 2022-11-02
    • 2012-05-20
    相关资源
    最近更新 更多