Skip to content

Instantly share code, notes, and snippets.

@tolbkni
Created January 14, 2014 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolbkni/b6032c3ed78e5f4bb180 to your computer and use it in GitHub Desktop.
Save tolbkni/b6032c3ed78e5f4bb180 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from flask import Flask
from flask import request, url_for, redirect
from werkzeug.utils import secure_filename
UPLOAD_DIR = "/tmp/uploads"
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_DIR
@app.route("/")
def hello_world():
return "Hello World"
@app.route("/upload", methods=["GET", "POST"])
def upload_file():
if request.method == "POST":
file = request.files["file"]
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
return redirect(
url_for('uploaded_file', file=file))
return '''
<!DOCTYPE html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
'''
@app.route("/upload/<file>")
def uploaded_file(file):
return "length={0:d}, file={1:s}".format(file.content_length, file)
if __name__ == "__main__":
app.run(port=10080, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment