python请求带有标头和参数的POST

时间:2018-07-01 15:11:59

标签: python python-3.x post request python-requests

我有一个发布请求,我正在尝试使用python中的requests发送。但是我收到无效的403错误。这些请求可以通过浏览器正常运行。

POST /ajax-load-system HTTP/1.1
Host: xyz.website.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-GB,en;q=0.5
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Referer: http://xyz.website.com/help-me/ZYc5Yn
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 56
Cookie: csrf_cookie_name=a3f8adecbf11e29c006d9817be96e8d4; ci_session=ba92hlh6o0ns7f20t4bsgjt0uqfdmdtl; _ga=GA1.2.1535910352.1530452604; _gid=GA1.2.1416631165.1530452604; _gat_gtag_UA_21820217_30=1
Connection: close

csrf_test_name=a3f8adecbf11e29c006d9817be96e8d4&vID=9999

我在python中尝试的是:

import requests
import json

url = 'http://xyz.website.com/ajax-load-system'

payload = {
'Host': 'xyz.website.com',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-GB,en;q=0.5',
'Referer': 'http://xyz.website.com/help-me/ZYc5Yn',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Content-Length': '56',
'Cookie': 'csrf_cookie_name=a3f8adecbf11e29c006d9817be96e8d4; ci_session=ba92hlh6o0ns7f20t4bsgjt0uqfdmdtl; _ga=GA1.2.1535910352.1530452604; _gid=GA1.2.1416631165.1530452604; _gat_gtag_UA_21820217_30=1',
'Connection': 'close',
'csrf_test_name': 'a3f8adecbf11e29c006d9817be96e8d4',
'vID': '9999',
}    

headers = {}

r = requests.post(url, headers=headers, data=json.dumps(payload))
print(r.status_code)  

但这正在打印403错误代码。我在这里做错了什么?

我期望返回响应为json:

{"status_message":"Thanks for help.","help_count":"141","status":true}

1 个答案:

答案 0 :(得分:4)

您混淆了标头和有效负载,有效负载是未JSON编码

这些都是标题:

Host: xyz.website.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-GB,en;q=0.5
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Referer: http://xyz.website.com/help-me/ZYc5Yn
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 56
Cookie: csrf_cookie_name=a3f8adecbf11e29c006d9817be96e8d4; ci_session=ba92hlh6o0ns7f20t4bsgjt0uqfdmdtl; _ga=GA1.2.1535910352.1530452604; _gid=GA1.2.1416631165.1530452604; _gat_gtag_UA_21820217_30=1
Connection: close

其中大多数是自动化的,不需要手动设置。 requests会根据URL为您设置HostAccept设置为可接受的默认值,在这些情况下Accept-Language几乎不需要Referer,除非使用HTTPS时,出于隐私方面的原因,甚至通常不会设置或过滤掉HTTPS,因此网站不再依赖于设置,Content-Type实际上必须反映您的POST的内容(并且不是JSON!),因此requests根据您的调用方式为您设置此值,Content-Length必须反映实际的内容长度,因此requests对其进行设置,因为它是计算此内容的最佳位置,并且Connection绝对应该由该库处理,因为您不想阻止它有效地重用连接。

充其量,您可以设置X-Requested-WithUser-Agent,但前提是服务器不会接受该请求。 Cookies标头反映了浏览器保存的cookie的值。您的脚本可以通过使用requests Session objectGET标头中指定的网址(或同一网址上的其他合适的网址)发出初始Referer请求,从服务器上获取自己的Cookie组。站点),此时服务器应在响应中设置cookie,然后将cookie存储在会话中以在发布请求中重用。使用该机制来获取您自己的CSRF Coo​​kie值。

请注意Content-Type标头:

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

当您将字典传递给data函数的requests.post()关键字时,该库将为您将数据编码为与该内容类型完全相同的内容。

实际有效载荷是

csrf_test_name=a3f8adecbf11e29c006d9817be96e8d4&vID=9999

这是csrf_test_namevID这两个字段,它们是您的payload词典的一部分。

请注意,csrf_test_name与cookie中的csrf_cookie_name值匹配。网站就是通过这种方式保护自己免受Cross-site forgery attacks的侵害,在这种情况下,第三方可能会尝试代表您发布到相同的URL。这样的第三方将无法访问相同的cookie,因此将被阻止。 您的代码需要获取一个新的cookie ;正确的CSRF实施会限制任何CSRF cookie可以重复使用的时间。

因此至少需要 才能使其全部正常工作是:

# *optional*, the site may not care about these. If they *do* care, then
# they care about keeping out automated scripts and could in future 
# raise the stakes and require more 'browser-like' markers. Ask yourself
# if you want to anger the site owners and get into an arms race.
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
    'X-Requested-With': 'XMLHttpRequest',
}

payload = {
    'vID': 9999,
}

url = 'http://xyz.website.com/ajax-load-system'
# the URL from the Referer header, but others at the site would probably
# also work
initial_url = 'http://xyz.website.com/help-me/ZYc5Yn'

with requests.Session() as session:
    # obtain CSRF cookie
    initial_response  = session.get(initial_url)
    payload['csrf_test_name'] = session.cookies['csrf_cookie_name']

    # Now actually post with the correct CSRF cookie
    response = session.post(url, headers=headers, data=payload)

如果这仍然导致问题,则需要尝试另外两个标头AcceptAccept-Language。考虑到这一点,这意味着该站点已经对如何使自动刮板机保持 进行了长时间的认真思考。考虑与他们联系并询问他们是否提供API选项。

相关问题