: This article mainly introduces [nginx] how to download statistics files (flask). For more information about PHP tutorials, see.
One requirement is whether the statistical file is completely downloaded by the user. because it is a web application, the implementation scheme is not found in js, so the nginx implementation scheme is searched and the simple exploration process is recorded.
Lab 1
- The original idea is to check the log. after downloading a file, we can check whether the file size transmitted by the log is consistent with the original file size.
- Test the size of the file to be downloaded.
- You can compare the size of transmitted bytes to determine whether a complete download log is used with a non-downloaded log.
This method is used for statistics based on logs. it is troublesome and time-sensitive to analyze logs at intervals.
Lab 2:
Found related blog
General process:
The main task is
1. modify the nginx configuration and forward the downloaded file information to the statistics service or url.
2. collect statistics on service records and determine the file download status
The focus here is to use the post_action parameter of nginx. after the download request is complete, the download status is sent to another statistical service. the statistics service determines the file download status.
Similar configuration
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;}
Then write a flask to receive statistics requests.
#! /Usr/bin/python #-*-coding: UTF-8-*-############################ File Name: counting_file.py # Author: orangleliu # Mail: orangleliu@gmail.com # Created Time: 16:41:05 # License: MIT ########################### '''nginx statistics on user download byte'' 'from flask import Flask, request app = Flask (_ name _) @ app. route ("/counting") defcounting (): req = request. args. get ("FileName") clientip = request. args. get ("ClientIP") size = request. args. get ("body_bytes_sent") status = request. args. get ("status") print "request", req print "ip", clientip print "size", size print "status ", status return "OK" if _ name _ = "_ main _": app. run (port = 8888, debug = True)
Access logs
lzz@ubuntu:code$ python counting_file.py * Running on http://127.0.0.1:8888/ * Restarting with reloaderrequest /index.htmlip 10.0.1.16size 0status OK127.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.zipip 10.0.1.16size 225280status127.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 -
You only need to process in flask to collect statistics on user downloads.
As mentioned in the above article, when users use multiple connections for download, there may be problems, repeated statistics, and inaccurate results, so there is still much room for improvement.
Statement:
This article from the "orangleliu notebook" blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/44219213
Author orangleliu adopts signature-non-commercial use-share protocol in the same way