源代码备份
This commit is contained in:
26
py/TcpDownload.py
Normal file
26
py/TcpDownload.py
Normal file
@ -0,0 +1,26 @@
|
||||
import socketserver
|
||||
|
||||
|
||||
class TCPHandler(socketserver.StreamRequestHandler):
|
||||
|
||||
BASE_PATH = "/Users/aria/temp/tcp/"
|
||||
|
||||
def handle(self):
|
||||
data = self.request.recv(1024).strip()
|
||||
file_name = data.decode("utf-8")
|
||||
print("file_name: %s" % file_name)
|
||||
print("{} wrote:".format(self.client_address[0]))
|
||||
with open(self.BASE_PATH + file_name, "rb") as f:
|
||||
b = f.read(1024)
|
||||
if b:
|
||||
self.wfile.write(b)
|
||||
else:
|
||||
print("发送成功")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
HOST, PORT = "localhost", 9999
|
||||
|
||||
with socketserver.TCPServer((HOST, PORT), TCPHandler) as server:
|
||||
server.serve_forever()
|
36
py/download.py
Normal file
36
py/download.py
Normal file
@ -0,0 +1,36 @@
|
||||
# coding=utf-8
|
||||
#!/usr/bin/python3
|
||||
|
||||
import os
|
||||
from flask import Flask, send_from_directory, request
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/download/<path:filename>", methods=['POST', 'GET'])
|
||||
def downloader(filename):
|
||||
"""
|
||||
不支持断点的下载
|
||||
"""
|
||||
data = request.values.get('key')
|
||||
print(data)
|
||||
dirpath = '/Users/aria/dev/ftp'
|
||||
# as_attachment=True 一定要写,不然会变成打开,而不是下载
|
||||
return send_from_directory(dirpath, filename, as_attachment=True)
|
||||
|
||||
|
||||
@app.route("/download1", methods=['POST', 'GET'])
|
||||
def downloader1():
|
||||
"""
|
||||
不支持断点的下载
|
||||
"""
|
||||
filename = request.values.get('filename')
|
||||
data = request.values.get('key')
|
||||
print(data)
|
||||
dirpath = 'D:/test'
|
||||
# as_attachment=True 一定要写,不然会变成打开,而不是下载
|
||||
return send_from_directory(dirpath, filename, as_attachment=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', debug=True) # 需要关闭防火墙
|
63
py/upload.py
Normal file
63
py/upload.py
Normal file
@ -0,0 +1,63 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
from flask import Flask, request, url_for, send_from_directory
|
||||
from werkzeug import secure_filename
|
||||
|
||||
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'rar', 'apk', 'zip'])
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = '/Users/aria/temp/test/'
|
||||
app.config['MAX_CONTENT_LENGTH'] = 1600 * 1024 * 1024
|
||||
|
||||
"""
|
||||
可以选择这个扩展
|
||||
Flask-Uploads
|
||||
"""
|
||||
|
||||
html = '''
|
||||
<!DOCTYPE html>
|
||||
<title>Upload File</title>
|
||||
<h1>图片上传</h1>
|
||||
<form method=post enctype=multipart/form-data>
|
||||
<input type=file name=file>
|
||||
<input type=submit value=上传>
|
||||
</form>
|
||||
'''
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route('/uploads/<filename>')
|
||||
def uploaded_file(filename):
|
||||
return send_from_directory(app.config['UPLOAD_FOLDER'],
|
||||
filename)
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def test():
|
||||
return'test'
|
||||
|
||||
|
||||
@app.route('/upload/', methods=['GET', 'POST'])
|
||||
def upload_file():
|
||||
if request.method == 'POST':
|
||||
print(request.values)
|
||||
print('params = ' + request.values.get('params'))
|
||||
|
||||
file = request.files['file']
|
||||
print(file)
|
||||
#if file and allowed_file(file.filename):
|
||||
print('start save')
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||||
# file_url = url_for('uploaded_file', filename=filename)
|
||||
# return html + '<br><img src=' + file_url + '>'
|
||||
return '200'
|
||||
return '405'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
Reference in New Issue
Block a user