2017-04-08 227 views
4

我無法理解請求。 比方說,我有這樣的要求:用Python發送請求(用Burp截獲)

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有效的請求? 我發現並不是很有幫助。我需要有人向我展示我給你的數據。

回答

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) 

是的,你會使用cookies登錄。Cookies是標題的一部分。

+0

那麼,Content-Length不能是一個字符串? (「27」) – David

+0

我會保持它作爲整數,但一個字符串是好的。 – apoorlydrawnape

+0

python錯誤只是告訴我它必須是一個字符串。無論如何,它似乎像網站有一些cookie存儲一些東西,如時間/日期和會話令牌,最有可能防止botting ...這將是艱難的。 – David

2

我不會寫詩,我只是給你一些exapmle代碼:

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) 

在您需要的設定等標題你得到它,我覺得頭;)

+0

謝謝,但最後一行應該在哪裏 - {「targetUser」:「12345」}在我的情況下呢?它似乎與其餘的不同。 – David

+0

在你的情況下,你應該改變「有些數據」爲「{」targetUser「:」12345「}」。 在數據變量中,你應該插入你的數據:) – Symonen