每当在Flask App中请求静态文件时,捕获请求标头

问题描述 投票:0回答:2

全部,

我能够在向端点发出请求时捕获请求头,但是我想知道如何在请求静态文件时捕获请求头。

例如,每当请求通过此端点获取图像时,我都可以将标头写入带有如下所示时间戳的.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'),但不确定如何实现此功能并捕获请求标头。

[还有一件事,其中提到的是,静态文件是从网络服务器提供的,例如NginxApache,而不是从烧瓶应用程序提供的,如果如上所述直接请求静态文件。如果是这样,是否可以在Web服务器级别捕获这些静态请求标头?

仅供参考:该应用程序是使用Flask,Python 3构建的,并使用来自Github的CI / CD部署到了Heroku。

关于此问题的任何帮助,或者如果有人可以指向我可以阅读和实现的资源,这将对他们非常有用。

提前感谢!

python flask static http-headers request-headers
2个回答
1
投票

好的,我要回答

((a)每当在Flask App中请求静态文件时捕获请求标头?

@app.route("/static/<var1>/<var2>")
def test(var1,var2):
    print(request.headers)
    return "hehe"

让我知道这是否对您有用,并回答您问题的(a)部分


0
投票

这是我一直在寻找的更新代码:

@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调查了!

© www.soinside.com 2019 - 2024. All rights reserved.