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


问题内容

我有一个发帖请求,试图requests在python中发送。但是我收到无效的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}


问题答案:

您在混淆标头和有效负载,有效负载 不是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

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

充其量 您可以设置X-Requested-WithUser- Agent,但前提是服务器不会接受该请求。该Cookies头反映的cookie浏览器保存的值。您的脚本可以通过使用请求会话对象GETReferer标头(或同一站点上的其他合适的URL)中发起的URL发出初始请求,来从服务器获取其自己的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_name以及vID,那需要你的一部分payload字典。

请注意,该csrf_test_namecsrf_cookie_namecookie_中的值 _匹配
。这是网站保护自己免受跨站点伪造攻击的方式,在这种攻击中,第三方可能会代表您尝试发布到相同的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选项。