4

我无法理解请求。假设我有这个要求:

POST /user/follow HTTP/1.1
Host: www.website.com
User-Agent: some user agent
Accept: application/json, text/plain, */*
Accept-Language: pl,en-US;q=0.7,en;q=0.3
Referer: https://www.website.com/users/12345/profile
Content-Type: application/json;charset=utf-8
X-CSRF-TOKEN: Ab1/2cde3fGH
Content-Length: 27
Cookie: some-cookie=;
DNT: 1
Connection: close

{"targetUser":"12345"}

我应该如何使用这些信息使用 python 发送有效请求?我发现的东西并没有真正的帮助。我需要有人用我给你的数据给我看一个例子。

4

3 回答 3

3

我会做这样的事情。

import requests
headers = {
    "User-Agent": "some user agent",
    "Content-Length": 27
    # you get the point
     }
data = {
    "targetUser" : "12345"
    }
url = "www.website.com/user/follow"
r = requests.post(url, headers=headers,data=data)

是的,您将使用 cookie 登录。cookie 是标题的一部分。

于 2017-04-08T20:17:21.627 回答
2

我不会写诗我只是给你一些示例代码:

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Referer": "SOMETHING",
    "Cookie": "SOMETHING",
    "Connection": "close",
    "Content-Type": "application/x-www-form-urlencoded"
}
data = "SOME DATA"
url = "https://example.com/something"

request = requests.post(url, headers=headers, data=data)

在标题中,您设置了所需的标题等。我认为您明白了;)

于 2017-04-08T20:14:40.250 回答
-2

这个 Burp 扩展可能会有所帮助:Copy As Python-Requests

它可以将选定的请求复制为 Python-Requests 调用。

在您的情况下,在复制为 Python-Requests 后,您将获得:

import requests

burp0_url = "http://www.website.com:80/user/follow"
burp0_cookies = {"some-cookie": ""}
burp0_headers = {"User-Agent": "some user agent", "Accept": "application/json, text/plain, */*", "Accept-Language": "pl,en-US;q=0.7,en;q=0.3", "Referer": "https://www.website.com/users/12345/profile", "Content-Type": "application/json;charset=utf-8", "X-CSRF-TOKEN": "Ab1/2cde3fGH", "DNT": "1", "Connection": "close"}
burp0_json={"targetUser": "12345"}
requests.post(burp0_url, headers=burp0_headers, cookies=burp0_cookies, json=burp0_json)
于 2021-12-24T04:42:25.743 回答