用cookies发送帖子请求[python]

时间:2017-03-02 20:41:35

标签: python http

POST /search HTTP/1.1
Host: chatango.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: cookies_enabled.chatango.com=yes; fph.chatango.com=http; id.chatango.com=programmable; auth.chatango.com={MY AUTH KEY - I already have this}
Connection: keep-alive
Referer: http://st.chatango.com/flash/sellers_external.swf
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

s=B&ama=99&ami=13&t=25&f=20

我真的很想知道如何通过python发送它,除了发送数据部分之外我什么都没找到,我真的不明白我应该如何发送cookie数据,因为我已经存储了到我通过API获得的变量,通过套接字获取它。

1 个答案:

答案 0 :(得分:0)

您可以在request()方法中添加新标头:

HTTPConnection.request(method, url[, body[, headers]])

请参阅request documentation

要添加Cookie,只需添加Cookie标头。

以下是Python site

中的POST示例
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()
print response.status, response.reason
data = response.read()
conn.close()