全,
每当向端点发出请求时,我都能够捕获请求头,但我想知道如何在请求静态文件时捕获请求头。
例如,每当请求通过此端点获取图像时,我都可以将头文件写入带有如下所示时间戳的.txt文件。
https://<host_name>/img
请求头部示例:
============================
28/05/2020, 14:31:03
Request Headers:
Host: <host_name>
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,kn;q=0.8
X-Request-Id: 00a79a7f-84eb-4fb3-b949-76254a93a001
X-Forwarded-For: 157.49.191.87
X-Forwarded-Proto: https
X-Forwarded-Port: 443
Via: 1.1 vegur
Connect-Time: 0
X-Request-Start: 1590676263802
Total-Route-Time: 0
============================但是当有人像这样直接访问静态对象时:
https://<host_name>/static/img/<img_name>.png在没有任何路由或视图作为上述端点的情况下,如何在直接请求时捕获静态对象的请求头?
目前,我正在使用Flask中的request.headers捕获请求头。我的img端点函数如下所示:
@app.route('/img')
def img_func():
req_headers = request.headers
dir = "static"
full_path = os.path.join(dir, "logs")
filename = full_path +'/request_headers_img.txt'
if os.path.exists(filename):
append_write = 'a' # append if already exists
else:
append_write = 'w' # make a new file if not
now = datetime.datetime.now()
date_time = now.strftime("%d/%m/%Y, %H:%M:%S")
app_logs = open(filename, append_write)
app_logs.write("============================" + '\n')
app_logs.write(date_time + '\n')
app_logs.write("Request Headers: " + '\n' + str(req_headers))
app_logs.write("============================"+ '\n')
app_logs.close()
fn = os.path.join(dir, "img") + '/<file_name>.png'
return send_file(fn)当我查看一些在线链接时,有人提到使用request.path('static'),但不确定如何实现这一点&捕获请求头。
这里提到的另一件事是,静态文件是从Nginx或Apache这样的was服务器提供的,而不是从flask应用程序提供的,就像我上面提到的那样直接请求静态文件。如果是这样的话,有没有办法在way服务器级别捕获这些静态请求头?
仅供参考:该应用程序是使用Flask,Python 3构建的,并使用Github的CI/CD部署到Heroku。
任何关于这方面的帮助,或者如果有人可以指向我可以阅读和实现这一点的资源,都将是非常有用的家伙。
提前感谢!
发布于 2020-05-29 03:39:58
好的,我要回答
(a)在Flask App中请求静态文件时捕获请求头?
@app.route("/static/<var1>/<var2>")
def test(var1,var2):
print(request.headers)
return "hehe"请让我知道这对你是否有效,并回答你问题的(a)部分。
发布于 2020-05-29 22:27:48
以下是我正在寻找的更新代码:
@app.route('/sta/<path:path>')
def getStaticFile(path):
fn = str(path).split("/")[-1]
file_type = str(fn).split(".")[-1]
dir = "static"
full_path = os.path.join(dir, "logs")
if file_type == 'png':
req_headers = request.headers
filename = full_path + '/request_headers_img.txt'
if os.path.exists(filename):
append_write = 'a' # append if already exists
else:
append_write = 'w' # make a new file if not
now = datetime.datetime.now()
date_time = now.strftime("%d/%m/%Y, %H:%M:%S")
app_logs = open(filename, append_write)
app_logs.write("============================" + '\n')
app_logs.write(date_time + '\n')
app_logs.write("Request Headers: " + '\n' + str(req_headers))
app_logs.write("============================" + '\n')
app_logs.close()
return send_from_directory('static', path)我应该使用<path:path>而不是<var1>/<var2>。谢谢,Akib对它的研究!
https://stackoverflow.com/questions/62067691
复制相似问题