[英]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.