2014-04-14 86 views
2

由於谷歌沒有發現任何關於錯誤「http.client.HTTPException:得到超過100個標頭」,我創建了這個問題。http.client.HTTPException:得到超過100個標頭

>>> import http.client as h 
>>> conn = h.HTTPConnection("www.coursefinders.com") 
>>> conn.request("HEAD","/") 
>>> conn.getresponse(); 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python3.4/http/client.py", line 1148, in getresponse 
    response.begin() 
    File "/usr/lib/python3.4/http/client.py", line 376, in begin 
    self.headers = self.msg = parse_headers(self.fp) 
    File "/usr/lib/python3.4/http/client.py", line 267, in parse_headers 
    raise HTTPException("got more than %d headers" % _MAXHEADERS) 
http.client.HTTPException: got more than 100 headers 

這個異常是什麼意思,我該如何正確處理這種類型的錯誤?網站在瀏覽器中正常工作。

回答

4

這裏是不涉及改變圖書館的PY文件的解決方案:

import httplib # or http.client if you're on Python 3 
httplib._MAXHEADERS = 1000 

剛把它放在代碼頂部

2

我打算建議使用requests,但是它使用http.client實現,並且出於同樣的原因失敗。要驗證問題是否是在圖書館或服務器,我嘗試了telnet會話,結果相似:

Trying 91.250.81.121... 
Connected to www.coursefinders.com. 
Escape character is '^]'. 
HEAD/HTTP\1.1 

HTTP/1.1 200 OK 
Date: Mon, 14 Apr 2014 08:35:54 GMT 
Server: Apache/2.2.16 (Debian) 
X-Powered-By: PHP/5.3.3-7+squeeze19 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Set-Cookie: PHPSESSID=2bnr4dpa4e90r2lmbv01smu1b6; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: c_id=496cc5d32486ac8d944e971ad6ec9eb3649ab23cs%3A3%3A%22235%22%3B; expires=Tue, 15-Apr-2014 08:35:54 GMT; path=/ 
Set-Cookie: login=-1; path=/ 
Set-Cookie: wc=1; expires=Thu, 09-Apr-2015 08:35:54 GMT 
Set-Cookie: login=-1; path=/ 
Set-Cookie: login=-1; path=/ 
[... Many Set-Cookie commands omitted ...] 
Set-Cookie: login=-1; path=/ 
Cache-Control: max-age=1, private, must-revalidate 
Vary: Accept-Encoding 
Connection: close 
Content-Type: text/html; charset=utf-8 

Connection closed by foreign host. 

所以看起來他們的服務器配置錯誤,並噴涌而出大量多餘的Set-Cookie頭。

似乎沒有辦法將httplib配置爲接受大量標題。我試圖尋找替代的HTTP庫,但沒有使用httplib實現,但沒有任何運氣。

+0

但是,輸出中的標題大小甚至不會接近100行。這個錯誤很奇怪。 – gadelat

+1

我表示我省略了很多輸出,因爲它是多餘的。我仍然沒有找到一個不依賴於httplib.client的庫。 –

4

變 「_MAXHEADERS」 到1000或10000在C值:\ Python27 \ LIB \ httplib.py

+0

我建議不要修改Python庫。你以後如何部署它?試試這個:http://stackoverflow.com/a/38815673/126574 – VitalyB

0

一個OSX我將此添加到我的代碼

import httplib as http_client 

然後調試腳本找到圖書館從哪裏加載。在我的情況下,它是

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py 

然後我編輯的限制按照Felix的後

sudo vim /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py