国外vps如果只用来科学上网,那每个月几百G的流量可能有些浪费了
在国内下载国外文件网速比较慢时,我们可以借助vps来帮助我们下载文件

理想的使用方式

  1. 不需要登录,功能尽可能简单,能方便下载
  2. 借助一个url就能实现借助vps下载文件
  3. vps只做下载文件中转功能

添加了前端UI界面

链接为 http://vps.kkwen.cn
示例

实现

python代码 vps_transfer_download.py

from flask import Flask, abort, Response, request
import os
import requests

app = Flask(__name__)
app.config["DEBUG"]  = False

file_obj = None

def generate(file_obj):
    while True:
        content = file_obj.raw.read(102400)
        if content:
            yield content
        else:
            break

@app.route('/vps', methods=['GET'])
def download_file():
    try:
        url = request.args.get('url')
        filename = os.path.basename(url)
        file_obj = requests.get(url, stream=True)
        headers = file_obj.headers

        s = Response(generate(file_obj), content_type=headers.get('Content-Type'))

        for k, v in headers.items():
            s.headers[k] = v
        s.headers['Content-Disposition'] = "attachment; filename={}".format(filename)
        return s

    except Exception as e:
        if file_obj is not None:
            file_obj.close()
        Response.close()
        abort(404)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

Nginx的Server段配置 f_vps_transfer_download.conf

server {
  listen 80;
  server_name f.kkwen.cn;

  location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header   Host             $host;
        proxy_set_header   Referer          $http_referer;
        proxy_set_header   Cookie           $http_cookie;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_redirect off;
    }
}

手动部署

部署python

先安装python模块requests和flask
]# pip install requests flask
]# nohup python vps_transfer_download.py &

部署Nginx

将f_vps_transfer_download.conf放置在nginx的conf.d目录下,然后执行 nginx -s reload

使用Docker部署

]# docker run -it -d -e "NGX_DOMAIN=your.domain.name" --rm -p 80:80 --name downloader aik8s/vps-downloader:v0.2

最后修改:2019 年 07 月 25 日 04 : 34 PM