[Nginx] how to download statistics files (flask)

Source: Internet
Author: User
: 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

  • Counting-100-completed-downloads
    Use post_action (the following method)

  • Nginx-post-action-to-trigger-successfully-download-file problems 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 in Chinese

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.