4
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()

andをコメントアウトするputheaderendheaders、正常に動作します。しかし、私はそれが生き続ける必要があります。

誰かが私が間違ったことを知っていますか?

4

2 に答える 2

9

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()
于 2010-08-13T03:11:02.547 に答える
-1

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、Gecko など) Chrome/51.0.2704.103 Safari/537.36"};

コード+

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

于 2016-07-19T16:39:12.397 に答える