简体   繁体   English

Python 请求标头不起作用 - 检查 Chrome 开发者工具 -> 网络

[英]Python requests header not working - Checked Chrome developer tools -> Network

This is my python code:这是我的python代码:

import requests
from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    url = 'https://google.co.in'
    headers = {
        'User-Agent':'blahblahblah'
    }
    r = requests.get(url, headers=headers)

    return 'check terminal'

So this is the way to change request headers in python requests.所以这是在python请求中更改请求头的方法。 But if I open the url and see developer options > Network > Request headers.但是,如果我打开 url 并看到开发人员选项 > 网络 > 请求标头。 I see default as user agent.我看到默认为用户代理。 Means it simply doesn't work.意味着它根本不起作用。

The request you're making is by the server, not the client (the web browser).您发出的请求是服务器发出的,而不是客户端(Web 浏览器)发出的。 The index page served by flask goes to a client.由flask 提供的index页面转到客户端。 The client doesn't make the requests.get request you've written here, instead the server does.客户端不会发出您在此处编写的requests.get请求,而是由服务器发出。

Instead, the client only requests whatever you're returning from the route, which here is 'check terminal' (which should not work, and should be something in the lines of return jsonify(result='check terminal') ), and is not concerned about what the server is doing internally.相反,客户端只请求您从路由返回的任何内容,这里是'check terminal' (它不应该工作,并且应该是return jsonify(result='check terminal') ),并且是不关心服务器在内部做什么。

So as @brunns has commented, these two requests are different.所以正如@brunns 所评论的,这两个请求是不同的。 If you want to check the headers of your request, maybe try httpbin.org .如果您想检查请求的标头,请尝试httpbin.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 python 访问谷歌浏览器开发者工具的网络选项卡吗? - can I access to network tab of google chrome developer tools with python? 如何使用 python 在 chrome 开发人员工具中访问网络选项卡 - How to access network tab in chrome developer tools using python Chrome 开发人员选项未收到 XHR 网络请求 - Chrome Developer Options not getting XHR network requests 一些http请求未出现在chrome开发人员工具中 - some http requests don't appear in chrome developer tools 是否可以使用请求从 chrome 工具 > 网络 > 预览中获取数据? - Is it possible to get data from chrome tools > network > preview using requests? Python selenium 获取“开发者工具”→网络→媒体日志 - Python selenium get "Developer Tools" →Network→Media logs Cookie未显示在Chrome开发者工具(Django Python)中 - Cookie does not show up in Chrome Developer Tools (Django Python) 如何使用 python selenium 访问 google chrome 开发人员工具上的安全面板? - How to access Security panel on google chrome developer tools with python selenium? 来自谷歌开发者工具的 JSON 链接在 Python(或浏览器)中不起作用 - JSON link from google developer tools not working in Python (or in browser) Chrome开发者工具找不到HttpRequest - Chrome developer tools cannot find HttpRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM