Skip to content

Instantly share code, notes, and snippets.

@feifangit
Created March 28, 2013 21:33
Show Gist options
  • Save feifangit/5267001 to your computer and use it in GitHub Desktop.
Save feifangit/5267001 to your computer and use it in GitHub Desktop.
flask vs Nginx upload, server code
import sys
import os
import traceback
import hashlib
from flask import Flask, request, redirect, jsonify, abort
app = Flask(__name__)
_HERE = os.path.dirname(os.path.abspath(__file__))
def save_and_getmd5(stream, dst, buffer_size=16384):
md5 = hashlib.md5()
with open(dst, "wb") as dst:
while 1:
buf = stream.read(buffer_size)
if not buf:
break
dst.write(buf)
md5.update(buf)
return md5.hexdigest()
@app.route("/upload", methods=["POST"])
def nginxupload():
return jsonify({"done":True, "md5": request.form["fileobj.md5"]})
@app.route("/pyupload", methods=["POST"])
def myupload():
try:
f = request.files["fileobj"]
targetPath = os.path.join(_HERE, "MarketX")
if not os.path.exists(targetPath):
os.makedirs(targetPath)
md5val = save_and_getmd5(f, os.path.join(targetPath, f.filename))
f.close()
return jsonify({"done":True, "md5": md5val})
except:
traceback.print_exc()
return jsonify({"done":False})
if __name__ == "__main__":
'''
gunicorn -k gevent -b 0.0.0.0:8222 -w 5 t:app
'''
workingPort = sys.argv[1]
app.run(host="0.0.0.0", port=int(workingPort), debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment