python: httplib 错误:无法发送标头

发布于 2024-09-14 07:36:38 字数 789 浏览 11 评论 0原文

conn = httplib.HTTPConnection('thesite')
conn.request("GET","myurl")
conn.putheader('Connection','Keep-Alive')
#conn.putheader('User-Agent','Mozilla/5.0(Windows; u; windows NT 6.1;en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome//5.0.375.126 Safari//5.33.4')
#conn.putheader('Accept-Encoding','gzip,deflate,sdch')
#conn.putheader('Accept-Language','en-US,en;q=0.8')
#conn.putheader('Accept-Charset','ISO-8859-1,utf-8;1=0.7,*;q=0.3')
conn.endheaders()
r1= conn.getresponse()

它会引发错误:

  conn.putheader('Connection','Keep-Alive')
  File "D:\Program Files\python\lib\httplib.py", line 891, in putheader
    raise CannotSendHeader()

如果我注释掉 putheaderendheaders,它运行正常。但我需要它保持活力。

有谁知道我做错了什么?

conn = httplib.HTTPConnection('thesite')
conn.request("GET","myurl")
conn.putheader('Connection','Keep-Alive')
#conn.putheader('User-Agent','Mozilla/5.0(Windows; u; windows NT 6.1;en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome//5.0.375.126 Safari//5.33.4')
#conn.putheader('Accept-Encoding','gzip,deflate,sdch')
#conn.putheader('Accept-Language','en-US,en;q=0.8')
#conn.putheader('Accept-Charset','ISO-8859-1,utf-8;1=0.7,*;q=0.3')
conn.endheaders()
r1= conn.getresponse()

It raises an error:

  conn.putheader('Connection','Keep-Alive')
  File "D:\Program Files\python\lib\httplib.py", line 891, in putheader
    raise CannotSendHeader()

If I comment out putheader and endheaders, it runs fine. But I need it to be keep alive.

Does anyone know what I did wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-09-21 07:36:38

使用 putrequest 而不是 request。由于 request 也可以发送标头,因此它会向服务器发送一个空行以指示标头结束,因此稍后发送标头会产生错误。

或者,您可以按照此处的方式进行操作:

import httplib, urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
conn.request("POST", "/cgi-bin/query", params, headers)
response = conn.getresponse()

Use putrequest instead of request. Since request also can send headers, it will send a blank line to the server to indicate end of headers, so sending headers afterward will create an error.

Alternatively, you could do as is done here:

import httplib, urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
conn.request("POST", "/cgi-bin/query", params, headers)
response = conn.getresponse()
陌上青苔 2024-09-21 07:36:38

headers = {"Content-Type":"application/x-www-form-urlencoded",
"连接":"保持活动",
"推荐人":"http://www.tu.sitio.cl/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36(KHTML,如 Gecko)Chrome/51.0.2704.103 Safari/537.36"};

代码+

conn.request(方法=“POST”,url=“/formulario/”,body=params,headers=headers)

headers = {"Content-Type":"application/x-www-form-urlencoded",
"Connection":"Keep-Alive",
"Referer":"http://www.tu.sitio.cl/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"};

code+

conn.request(method="POST", url="/formulario/", body=params, headers=headers)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文