1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#!/usr/bin/env python
"""File Upload
A simple example showing how to access an uploaded file.
"""
from circuits.web import Server, Controller
UPLOAD_FORM = """
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h1>Upload Form</h1>
<form method="POST" action="/" enctype="multipart/form-data">
Description: <input type="text" name="desc"><br>
<input type="file" name="file">
<input type="submit" value="Submit">
</form>
</body>
</html>
"""
UPLOADED_FILE = """
<html>
<head>
<title>Uploaded File</title>
</head>
<body>
<h1>Uploaded File</h1>
<p>
Filename: %s<br>
Description: %s
</p>
<p><b>File Contents:</b></p>
<pre>
%s
</pre>
</body>
</html>
"""
class Root(Controller):
def index(self, file=None, desc=""):
"""Request Handler
If we haven't received an uploaded file yet, repond with
the UPLOAD_FORM template. Otherwise respond with the
UPLOADED_FILE template. The file is accessed through
the ``file`` keyword argument and the description via
the ``desc`` keyword argument. These also happen to be
the same fields used on the form.
"""
if file is None:
return UPLOAD_FORM
else:
return UPLOADED_FILE % (file.filename, desc, file.value)
app = Server(("0.0.0.0", 8000))
Root().register(app)
app.run()
|