3

我正在编写一个 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 服务器,结果是一样的。

4

1 回答 1

3

两种解决方案共享一些像 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

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

于 2013-08-06T07:43:16.923 回答