Skip to content

Instantly share code, notes, and snippets.

@Arthraim
Created May 27, 2011 04:27
Show Gist options
  • Save Arthraim/994641 to your computer and use it in GitHub Desktop.
Save Arthraim/994641 to your computer and use it in GitHub Desktop.
a python web framework bottle's example
#coding: utf-8
from bottle import route, error, post, get, run, static_file, abort, redirect, response, request, template
@route('/')
@route('/index.html')
def index():
return '<a href="/hello">Go to Hello World page</a>'
@route('/hello')
def hello():
return '<h1>HELLO WOLRD</h1>'
@route('/hello/:name')
def hello_name(name):
page = request.GET.get('page', '1')
return '<h1>HELLO %s <br/>(%s)</h1>' % (name, page)
@route('/static/:filename')
def serve_static(filename):
return static_file(filename, root='/home/arthur/workspace/my_python_codes/src/')
@route('/raise_error')
def raise_error():
abort(404, "error...")
@route('/redirect')
def redirect_to_hello():
redirect('/hello')
@route('/ajax')
def ajax_response():
return {'dictionary': 'you will see ajax response right? Content-Type will be "application/json"'}
@error(404)
def error404(error):
return '404 error !!!!!'
@get('/upload')
def upload_view():
return """
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="file" name="data" />
<input type="submit" name="submit" value="upload now" />
</form>
"""
@post('/upload')
def do_upload():
name = request.forms.get('name')
data = request.files.get('data')
if name is not None and data is not None:
raw = data.file.read() # small files =.=
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
return "You missed a field."
@route('/tpl')
def tpl():
return template('test')
run(host='localhost', port=8000, reloader=True)
@spil-dave
Copy link

Thank you for the your code sample, it was very useful!
There is a minor typo in function upload_view():
In the form, mutipart should be muLtipart. The error appears on function do_upload() calls.
Because the form is not multipart, data = request.files.get('data') returns None.
Hope this helps, cheers, Dave

@Arthraim
Copy link
Author

Arthraim commented Apr 5, 2012

You're right! Careless mistake. Thanks @spil-dave.

@fcourchesne
Copy link

Very useful, thank you.

@careyshan
Copy link

This is very useful code, Just one quick question. How do you access a file from the do_upload function?
Many thanks for your help.

@kaklis
Copy link

kaklis commented Jun 17, 2013

Thank you for these great snippets!

@markhu
Copy link

markhu commented Apr 28, 2014

Thanks for the concise examples. One change I made was to variable-ize the localhost, since 0.0.0.0 usually works better for me, i.e. in the Nitrous.io environment. Also, why not have the main page link the rest so you don't have to type?

@Slapodyn
Copy link

Nice example- I'm trying to get my head around this reloader flag, as this has been known to give some errors but here it works cool.

@jayendra13
Copy link

How to redirect to hyperlink?

@rnaeye
Copy link

rnaeye commented Dec 8, 2017

thank you!

@arulrajnet
Copy link

Hey do u have any idea to create access log to a file. ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment