code is now working with py3

This commit is contained in:
makefu 2013-11-18 20:12:47 +01:00
parent 63f233e0b9
commit 1537273b09

View file

@ -1,9 +1,10 @@
from flask import Flask, Response, request from flask import Flask, Response, request
from functools import wraps from functools import wraps
#import sys import sys
import random import random
import string import string
storedir = "store" import os
storedir = "%s/../store" % os.path.dirname(os.path.abspath(sys.argv[0]))
app = Flask(__name__) app = Flask(__name__)
app.config['storedir'] = storedir app.config['storedir'] = storedir
@ -35,13 +36,14 @@ def init():
@returns_plain @returns_plain
@app.route("/", methods=["POST"]) @app.route("/", methods=["POST"])
def store_file(): def store_file():
try: #try:
data = request.form['p'] data = request.form['p']
key = generate_key() key = generate_key()
store(key, data) store(key, data)
return "http://%s/%s" % (request.host, key) return u"http://%s/%s\n" % (request.host, key)
except: #except Exception as e:
return usage(request.host), 401 # print(e)
# return usage(request.host), 401
@returns_plain @returns_plain
@ -55,19 +57,24 @@ def retrieve_file(key):
def store(key, value): def store(key, value):
with open("%s/%s" % (storedir, key), "wb+") as f: with open("%s/%s" % (storedir, key), "wb+") as f:
f.write(value) f.write(bytes(value, "UTF-8"))
def retrieve(key): def retrieve(key):
with open("%s/%s" % (storedir, key), "rb+") as f: with open("%s/%s" % (storedir, key), "rb+") as f:
return f.read() return f.read().decode("UTF-8")
def generate_key(): def generate_key():
s = string.lowercase+string.digits s = string.ascii_lowercase+string.digits
return ''.join(random.sample(s, 24)) return ''.join(random.sample(s, 10))
if __name__ == "__main__": if __name__ == "__main__":
#app.debug = True #app.debug = True
print("Storedir is %s" % storedir)
try:
os.mkdir(storedir)
except:
pass
app.run(host='0.0.0.0') app.run(host='0.0.0.0')