这些消息是对服务器的请求,所以我很确定来自 Flask 的 Requests 模块就是您要寻找的。p>
只需将 Request 对象(或您想要的属性)保存在每个函数的开头,这是一个 Flask 路由 (@app.route("XXX"))
我用我拥有的 Flask 应用程序做了一个小测试,这就是我得到的:
代码
from flask import request
@app.route('/')
def main():
print("HEHEHE: {}".format(request.headers))
@app.route('/getCharacters', methods=['GET'])
def getCharacters():
print("HIHIHII: {}".format(request.headers))
结果
HEHEHE: Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
127.0.0.1 - - [09/Feb/2018 08:59:23] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [09/Feb/2018 08:59:23] "GET /static/css/style.css HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 08:59:23] "GET /static/chart.min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 08:59:23] "GET /static/main.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 08:59:23] "GET /static/assets/movies/lotr_logo.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 08:59:23] "GET /static/assets/movies/sw_logo.png HTTP/1.1" 304 -
HIHIHII: Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: */*
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8000/
Connection: keep-alive
127.0.0.1 - - [09/Feb/2018 09:00:49] "GET /getCharacters?movie=sw HTTP/1.1" 200 -
127.0.0.1 - - [09/Feb/2018 09:00:49] "GET /static/assets/pjs/sw/han.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 09:00:49] "GET /static/assets/pjs/sw/vader.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 09:00:49] "GET /static/assets/pjs/sw/threepio.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 09:00:49] "GET /static/assets/pjs/sw/luke.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2018 09:00:49] "GET /static/assets/pjs/sw/leia.png HTTP/1.1" 304 -