httplib.HTTPSConnection在并发环境中导致SSL错误

时间:2012-02-17 20:22:59

标签: python ssl httplib

我制作了一个小的python脚本来对我上传之前一直在努力的网站进行压力测试。它通过每隔5分钟产生一个新线程来运行,该线程在一段时间(真实)循环中运行下面的代码。

conn = httplib.HTTPSConnection("site", 000, "pem", "pem", timeout = 30)
conn.request("GET", "/reports.php?" + url, headers = headers)
response = conn.getresponse()
read = (response.read())

如果我只有一个线程,则代码请求/响应每次都成功。当我创建新线程时,我会更频繁地得到以下错误。最终当有大约10个线程时,它会在90%的时间内失败。

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "stresstest.py", line 32, in threadproc
    stressTest(conn)
  File "stresstest.py", line 76, in stressTest
    response = conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1027, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 365, in _read_status
    line = self.fp.readline()
  File "/usr/lib/python2.7/socket.py", line 430, in readline
    data = recv(1)
  File "/usr/lib/python2.7/ssl.py", line 232, in recv
    return self.read(buflen)
  File "/usr/lib/python2.7/ssl.py", line 151, in read
    return self._sslobj.read(len)
SSLError: The read operation timed out

我想知道ssl代码httplib使用的是不是线程安全的还是有其他原因导致失败?

1 个答案:

答案 0 :(得分:2)

它是线程安全的。您可能想要开始研究服务器代码的性能......