简体   繁体   中英

Capture Request Headers whenever a static file is requested in Flask App

All,

I'm able to capture request headers whenever a request is made to an endpoint but I was wondering how to capture the request headers whenever a static file is requested.

For example, whenever a request is made to get an image through this endpoint, I'm able to write the headers to a.txt file with a timestamp like below.

https://<host_name>/img

Request Headers example:

============================
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

============================

But when someone directly accesses the static objects like this:

https://<host_name>/static/img/<img_name>.png

How to capture the request headers for the static objects when requested directly without any route or a view as the above endpoint?

Currently, I'm capturing the request headers using request.headers in Flask. My function for an img endpoint looks like this:

@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)

As I did check some online links, someone mentioned making use of request.path('static') but not sure on how to implement this & capture the request headers.

One more thing which was mentioned there that the static files are being served from the webserver like Nginx or Apache , not from the flask app if static files are requested directly as I mentioned above. If so is there a way to capture these static request headers at the webserver level?

FYI: The app is built using Flask, Python 3 & deployed to Heroku using CI/CD from Github.

Any help around this or if someone can point to the resources where I can read & implement this will be very useful guys.

Thanks in advance!

Ok so I am going to answer

(a) Capture Request Headers whenever a static file is requested in Flask App?

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

Let me know if that works for you, and answers part (a) of your question

Here is my updated code which I was looking for:

@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)

I should make use of <path:path> instead of <var1>/<var2> . Thanks, Akib for looking into it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM