python-requests - user-agent 正在被覆盖

python-requests - user-agent is being overriden

我有

    logindata = {
        'username': 'me',
        'password': 'blbla'
    }
    payload = {'from':'me', 'lang':'en', 'url':csv_url}
    headers = {
        'User-Agent': 'Mozilla/5.0'
    }
    api_url = 'http://dev.mypage.com/admin/app/import/'

    with requests.Session() as s:
        s.post(api_url, data=json.dumps(logindata), headers=headers)

        print s.headers

        # An authorised request.
        r = s.get(api_url, params=payload, headers=headers)

我被拒绝了,但这是因为 403 forbidden。然后我打印了 headers,我得到:

..'User-Agent': 'python-requests/2.2.1 CPython/2.7.5 Windows/7'..

为什么我的 'User-Agent': 'Mozilla/5.0' 被覆盖了?我在这里错过了什么?

headers 不会这样保存在session里面。

您需要在每次发出请求时显式传递它们,或者设置一次 s.headers

with requests.Session() as s:
    s.headers = {'User-Agent': 'Mozilla/5.0'}

您可以通过检查 response.request.headers:

来检查是否发送了正确的 headers
with requests.Session() as s:
    s.headers = {'User-Agent': 'Mozilla/5.0'}

    r = s.post(api_url, data=json.dumps(logindata))
    print(r.request.headers)

另请参阅您在 session object 上设置的 Session class is implemented - every time you make a request it merges the request.headers with headers:

headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),

如果您希望session对所有请求使用特定的headers,您需要在[=30]上设置那些headers =],明确地:

with requests.Session() as s:
    s.headers.update(headers)
    s.post(api_url, data=json.dumps(logindata))

    # An authorised request.
    r = s.get(api_url, params=payload)

s.headers.update(headers) 行将您的字典添加到 session headers。

会话从不复制来自 requests 的信息以重新用于其他请求。仅捕获来自响应(特别是 cookie)的信息以供重复使用。

有关详细信息,请参阅 requests Session Objects documentation:

Sessions can also be used to provide default data to the request methods. This is done by providing data to the properties on a Session object.