flask 文件下载 文件服务器 请求参数 函数修饰符
180M电子书
通过已经下载到pc,在上传到私有公网服务器,之后,再异地下载
以下代码,文件内容错乱
[root@bigdata-server-03 mydataweb]# tree . ├── mydl.py └── templates ├── hello.html └── 计算机网络第六版.pdf 1 directory, 3 files [root@bigdata-server-03 mydataweb]# cat mydl.py from flask import Flask, render_template myapp = Flask(__name__) @myapp.route('/hello/<name>') def hello(name=None): return render_template('hello.html', name=name) if __name__ == '__main__': myapp.run(host='0.0.0.0', debug=True) [root@bigdata-server-03 mydataweb]# cat templates/hello.html <!doctype html> <title>Hello from Flask</title> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello, World!</h1> {% endif %} <a href="./计算机网络第六版.pdf" download="计算机网络第六版.pdf">Download file</a> [root@bigdata-server-03 mydataweb]#
[root@bigdata-server-03 mydataweb]# du -Bm
87M ./templates
87M .
[root@bigdata-server-03 mydataweb]# du -Bm
87M ./templates
87M .
[root@bigdata-server-03 mydataweb]# du -Bk
89288K ./templates
89296K .
[root@bigdata-server-03 mydataweb]# du -Bk
89464K ./templates
89472K .
[root@bigdata-server-03 mydataweb]#
sftp
浏览器显示的文件地址为
http://ip:5000/hello/file
局域网测试时: http://192.168.2.51:5000/hello/a.txt
待优化方向:
在不要求公网ip的情况下,走局域网快速传输到手机
以下代码,测试了4K txt文件
from flask import Flask, send_from_directory
myapp = Flask(__name__)
@myapp.route('/hello/<name>')
def hello(name=None):
if name == 'cisco':
return send_from_directory('./', 'f.txt', as_attachment=True)
if __name__ == '__main__':
myapp.run(host='0.0.0.0', debug=True)
180M pdf文件未测试
服务脚本
from flask import Flask, send_from_directory myapp = Flask(__name__) @myapp.route('/myfile/<uid>') def myfile(uid=None): f = '{}logo.jpg'.format(uid) return send_from_directory('./myfile/', f, as_attachment=True) @myapp.route('/mycode/<imgname>') def mycode(imgname=None): f = imgname return send_from_directory('./mycode/', f, as_attachment=True) if __name__ == '__main__': myapp.run(host='0.0.0.0', debug=True)
文件下载
import requests url = 'http://192.168.6.22:5000/hello/cisco' url = 'http://192.168.6.22:5000/myfile/34054505' def dl_webimg(img_path, img_url, img_name): try: # ONLY 200 if requests.get(img_url).status_code == 200: bytes = requests.get(img_url)._content if bytes != 0: r = '{}{}'.format(img_path, img_name) with open(r, 'wb')as f: f.write(bytes) return True except Exception as e: logs = e print(logs) return False
192.168.3.103 - - [16/May/2018 19:38:44] "GET /myfile/34062376 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:38:49] "GET /myfile/50011457 HTTP/1.1" 404 - 192.168.2.157 - - [16/May/2018 19:38:54] "GET /myfile/50008187 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:38:54] "GET /myfile/50008187 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:39:32] "GET /myfile/50004237 HTTP/1.1" 404 - 192.168.3.103 - - [16/May/2018 19:40:36] "GET /myfile/50005924 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:40:36] "GET /myfile/50005924 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:40:39] "GET /myfile/50011844 HTTP/1.1" 404 - 192.168.3.103 - - [16/May/2018 19:41:23] "GET /myfile/33999180 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:41:23] "GET /myfile/33999180 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:41:27] "GET /myfile/33985565 HTTP/1.1" 404 - 192.168.2.157 - - [16/May/2018 19:41:28] "GET /myfile/50006768 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:41:28] "GET /myfile/50006768 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:41:31] "GET /myfile/50012105 HTTP/1.1" 404 - 192.168.3.102 - - [16/May/2018 19:41:48] "GET /myfile/34059578 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:41:48] "GET /myfile/34059578 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:41:56] "GET /myfile/50011842 HTTP/1.1" 404 - 192.168.3.102 - - [16/May/2018 19:41:57] "GET /myfile/50011596 HTTP/1.1" 404 - 192.168.2.40 - - [16/May/2018 19:42:26] "GET /myfile/34046392 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:42:26] "GET /myfile/34046392 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:42:40] "GET /myfile/50010681 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:42:40] "GET /myfile/50010681 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:42:40] "GET /myfile/34005188 HTTP/1.1" 404 - 192.168.2.40 - - [16/May/2018 19:42:42] "GET /myfile/50009318 HTTP/1.1" 404 - 192.168.3.102 - - [16/May/2018 19:42:49] "GET /myfile/34057076 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:42:49] "GET /myfile/34057076 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:42:57] "GET /myfile/50010796 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:42:57] "GET /myfile/50010796 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:42:57] "GET /myfile/50011625 HTTP/1.1" 404 - 192.168.2.157 - - [16/May/2018 19:43:05] "GET /myfile/34064430 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:43:05] "GET /myfile/34064430 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:43:07] "GET /myfile/34034844 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:43:07] "GET /myfile/34034844 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:43:11] "GET /myfile/33996397 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:43:11] "GET /myfile/33996397 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:43:20] "GET /myfile/50011577 HTTP/1.1" 404 - 192.168.2.40 - - [16/May/2018 19:43:25] "GET /myfile/50006165 HTTP/1.1" 404 - 192.168.3.103 - - [16/May/2018 19:43:56] "GET /myfile/34061589 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:43:56] "GET /myfile/34061589 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:44:03] "GET /myfile/34015391 HTTP/1.1" 404 - 192.168.3.103 - - [16/May/2018 19:44:07] "GET /myfile/50007988 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:44:07] "GET /myfile/50007988 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:44:10] "GET /myfile/50001587 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:44:10] "GET /myfile/50001587 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:44:14] "GET /myfile/50005155 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:44:14] "GET /myfile/50005155 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:44:19] "GET /myfile/50010215 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:44:20] "GET /myfile/50010215 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:44:23] "GET /myfile/50005051 HTTP/1.1" 404 - 192.168.3.103 - - [16/May/2018 19:44:27] "GET /myfile/50011528 HTTP/1.1" 404 - 192.168.2.40 - - [16/May/2018 19:44:43] "GET /myfile/34031512 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:44:43] "GET /myfile/34031512 HTTP/1.1" 200 - 192.168.2.40 - - [16/May/2018 19:44:52] "GET /myfile/50002485 HTTP/1.1" 404 - 192.168.2.157 - - [16/May/2018 19:44:55] "GET /myfile/50000148 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:44:55] "GET /myfile/50000148 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:44:57] "GET /myfile/34015601 HTTP/1.1" 404 - 192.168.3.102 - - [16/May/2018 19:45:02] "GET /myfile/34030027 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:02] "GET /myfile/34030027 HTTP/1.1" 200 - 192.168.2.157 - - [16/May/2018 19:45:05] "GET /myfile/50009356 HTTP/1.1" 404 - 192.168.3.102 - - [16/May/2018 19:45:12] "GET /myfile/34064430 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:12] "GET /myfile/34064430 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:19] "GET /myfile/50007083 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:19] "GET /myfile/50007083 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:22] "GET /myfile/50009027 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:22] "GET /myfile/50009027 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:27] "GET /myfile/34021924 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:27] "GET /myfile/34021924 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:31] "GET /myfile/50003822 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:31] "GET /myfile/50003822 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:38] "GET /myfile/50007274 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:38] "GET /myfile/50007274 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:45:41] "GET /myfile/34064093 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:45:41] "GET /myfile/34064093 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:41] "GET /myfile/34063466 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:41] "GET /myfile/34063466 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:45:45] "GET /myfile/50007149 HTTP/1.1" 200 - 192.168.3.103 - - [16/May/2018 19:45:45] "GET /myfile/50007149 HTTP/1.1" 200 - 192.168.3.102 - - [16/May/2018 19:45:48] "GET /myfile/34004441 HTTP/1.1" 404 - 192.168.3.103 - - [16/May/2018 19:45:51] "GET /myfile/34063434 HTTP/1.1" 200 -
flask笔记:14:获取所有post或者get请求参数 - CSDN博客 https://blog.csdn.net/u013055678/article/details/70214756
函数修饰符
python中的@ - 半天的半天 - 博客园 https://www.cnblogs.com/mxh1099/p/5685619.html