gpt4 book ai didi

python: httplib 错误: 无法发送 header

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:15 26 4
gpt4 key购买 nike

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,它运行正常。但我需要它来维持生命。

谁知道我做错了什么?

最佳答案

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

或者,您可以按原样做 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()

关于python: httplib 错误: 无法发送 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3473583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com