简体   繁体   English

发送带有Cookie的帖子请求[python]

[英]Send post request with cookies [python]

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

I'd really like to know how to send this via python, I haven't found anything except sending that data part, I really don't understand how I'm supposed to send the cookie data as I have it stored into a variable which I got through an API, which obtains it through sockets. 我真的很想知道如何通过python发送此消息,除了发送该数据部分,我什么都没找到,我真的不明白如何将cookie数据存储到变量中来发送cookie数据我是通过API获取的,该API是通过套接字获取的。

You can add new headers in the request() method: 您可以在request()方法中添加新的标头:

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

See request documentation . 请参阅请求文档

To add a cookie, just add the Cookie header. 要添加Cookie,只需添加Cookie标头。

Here is a POST example from the Python site : 这是Python站点的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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM