Home > Backend Development > PHP Tutorial > [nginx] Whether the statistical file download is complete (flask)

[nginx] Whether the statistical file download is complete (flask)

WBOY
Release: 2016-08-08 09:28:19
Original
1131 people have browsed it

There is a need to count whether the file has been completely downloaded by the user. Because it is a web application, no implementation solution was found using js, so I searched for the nginx implementation solution and recorded the simple exploration process.

Experiment 1

  • The most original idea is to check the log. After downloading a file, we check whether the file size of the log transfer is consistent with the original size of the file
  • Test the size of the file to be downloaded

  • A completely downloaded log and an incomplete download log can be judged by comparing the size of the transferred bytes

This method is to make statistics based on the logs, and analyze the logs at regular intervals to get the results. It's a bit troublesome and not timely.

Experiment 2:

Find relevant blogs

  • Counting-100-completed-downloads
    Problems encountered using post_action (the method used below)

  • Nginx-post-action-to-trigger-successfully-download-file nginx-post-action-proxy-pass-track-downloading-file

  • Use x-accel-redirect

    • nginx-x-accel-redirect-php-rails English
    • Use nginx’s x-accel-redirect header to implement download control Chinese

Approximate process:

The main jobs are 2
1 Modify the configuration of nginx and forward the downloaded file information to the statistical service or URL
2 The statistical service records and determines the file download status

The key point here is to use the post_action parameter of nginx. After the download request is completed, the download status is sent to another statistical service, and the statistical service determines the file download status

The configuration is similar

<code>location / {
    limit_rate 20k;
    post_action @afterdownload;
}

location @afterdownload {
    proxy_pass http://127.0.0.1:8888/counting?FileName=$uri&ClientIP=$remote_addr&body_bytes_sent=$body_bytes_sent&status=$request_completion;
    internal;
}
</code>
Copy after login

Then write a flask to receive the statistics request

<code><span>#!/usr/bin/python</span><span>#-*- coding:utf-8 -*-</span><span>############################</span><span>#File Name: counting_file.py</span><span>#Author: orangleliu</span><span>#Mail: orangleliu@gmail.com</span><span>#Created Time: 2015-03-11 16:41:05</span><span>#License: MIT</span><span>############################</span><span>'''
    nginx统计用户下载文件字节

    '''</span><span>from</span> flask <span>import</span> Flask, request
    app = Flask(__name__)

    <span>@app.route("/counting")</span><span><span>def</span><span>counting</span><span>()</span>:</span>
        req = request.args.get(<span>"FileName"</span>)
        clientip = request.args.get(<span>"ClientIP"</span>)
        size = request.args.get(<span>"body_bytes_sent"</span>)
        status = request.args.get(<span>"status"</span>)
        <span>print</span><span>"request  "</span>, req
        <span>print</span><span>"ip  "</span>, clientip
        <span>print</span><span>"size  "</span>, size
        <span>print</span><span>"status  "</span>, status
        <span>return</span><span>"ok"</span><span>if</span> __name__ == <span>"__main__"</span>:
        app.run(port=<span>8888</span>, debug=<span>True</span>)</code>
Copy after login

Access log

<code>lzz@ubuntu:code$ python counting_file.py
 * Running on http://127.0.0.1:8888/
 * Restarting with reloader
request   /index.html
ip   10.0.1.16
size   0
status   OK
127.0.0.1 - - [12/Mar/2015 10:42:59] "GET /counting?FileName=/index.html&ClientIP=10.0.1.16&body_bytes_sent=0&status=OK HTTP/1.0" 200 -
request   /Pillow-2.3.0.zip
ip   10.0.1.16
size   225280
status
127.0.0.1 - - [12/Mar/2015 10:43:14] "GET /counting?FileName=/Pillow-2.3.0.zip&ClientIP=10.0.1.16&body_bytes_sent=225280&status= HTTP/1.0" 200 -
</code>
Copy after login

As long as the processing is done in flask, the user downloads can be counted.
The above article also said that when users use multiple connections to download, there may be problems. Statistics will be repeated and the results will be inaccurate, so there is still a lot of room for improvement.

Statement:
This article comes from the "orangleliu Notebook" blog. Please be sure to keep this source for reprinting http://blog.csdn.net/orangleliu/article/details/44219213
Author orangleliu adopts Attribution-NonCommercial-ShareAlike License

The above introduces the complete idea of ​​downloading [nginx] statistics files (flask), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template