from bottle import route, run
@route('/hello')
def hello():
return "Hello World!"
run(host='localhost', port=8080, debug=True)
Default: use the global object;
But we can use our own object.
from bottle import Bottle, run
app = Bottle()
@app.route('/hello') #Use the "app" object
def hello():
return "Hello World!"
run(app, host = 'localhost', port = 8080) #Use the "app" object
from bottle import template
#Use two decorators
@route('/')
@route('/hello/<name>') #transmit the "name" variable to the function
def greet(name='stranger'):
return template('Hello , how are you?', NAME=name)
#Use template, and transmit the "NAME" variable by keyword argument.
run(host='localhost', port=8080)
<name:filter>
or <name:filter:config>
Filter:
@route('/object/<id:int>')
@route('/static/<path:path>')
@route('/show/<name:re:[a-z]+>')
from bottle import static_file
@route('/object/<id:int>')
def callback(id):
assert isinstance(id, int)
@route('/show/<name:re:[a-z]+>')
def callback(name):
assert name.isalpha()
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='/')
GET is the defalut method...
The argument method of route can be used...
The decorator get(), post(), put(), delete() and patch() are ok as well!
from bottle import get, post, request, route, run
#Get the HTML form
@get('/login') #or @route('/login')
def login():
return '''
<form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit" />
</form>
'''
#Get the value of the form that is posted
@post('/login') #or @route('/login', method='POST')
def do_login():
#Get the value of input whose name is "username" or "password" int the form
username = request.forms.get('username')
password = request.forms.get('password')
if check_login(username, password):
return "<p>Your login information was correct.</p>"
else:
return "<p>Login failed.</p>"
#Assume that the login is allowed
def check_login(username, password):
return 1
run(host='localhost', port=8080)
The function of static_file:
The first argument is the name of the static file...
You can give the root directory by using the argument root
from bottle import static_file
@route('/static/<filename:path>') #"path" means we can access the deeper directory but not only the root.
def server_static(filename):
return static_file(filename, root='/home/zk/joinus_raw/joinus_2015spring_raw/')
run(host='localhost', port=8080)
from bottle import static_file
@route('/images/<filename:re:.*\.png>')
def send_image(filename):
return static_file(filename, root='/home/zk/joinus_raw/joinus_2015spring_raw/image/',mimetype='image/png')
#Point out that the mimetype is "image/png", but generally it is not necessary
run(host='localhost', port=8080)
Most browsers will open the files directly if the mimetype is known...
If you want to force it to download the file, you can give the argument download
@route('/download/<filename:re:.*\.png>')
def download_image(filename):
return static_file(filename, root='/home/zk/joinus_raw/joinus_2015spring_raw/image/', download=filename)
#The value of download is the name of the file you want.
run(host='localhost', port=8080)
Most headers are unique, and we can use response.set_header to set them;
But some are allowed to appear more than once in a response, and we can use response.add_header to add more.
Both functions take 2 parameters, a header name and its value.
@route('/wiki/<page>')
def wiki(page):
response.set_header('Set-Cookie', 'name=value')
response.add_header('Set-Cookie', 'name2=value2')
We can use request.get_cookie to get the value of a cookie.
It takes a parament, the name of the cookie.
And use response.set_cookie to set a cookie.
It takes 2 paraments, the name of the cookie and its value.
More keyword arguments are allowed:
What's more, we can use keyword argument secret both in request.get_cookie and response.set_cookie to make the imformation in cookies securier.
@route('/hello')
def hello_again():
if request.get_cookie("visited", secret="hello"):
return "Welcome back! Nice to see you again"
else:
response.set_cookie("visited", "yes", secret="hello")
return "Hello there! Nice to meet you"
from bottle import request, route, template
request.cookies.xxx get the cookie named "xxx".
If "xxx" doesn't exist, return ''.
@route('hello')
def hello():
name = request.cookies.username or 'Guest'
return template('Hello ', name=name)
It is similar to getting the cookie.
But we can get all value of the form as a list by request.forms.getall.
for choice in request.forms.getall('multiple_choice'):
do_something(choice)
The ip is stored in the dictionary request.environ.
The key of ip is REMOTE_ADDR
@route('/my_ip')
def show_ip():
ip = request.environ.get('REMOTE_ADDR')
# or ip = request.get('REMOTE_ADDR')
# or ip = request['REMOTE_ADDR']
return template("Your IP is: ", ip=ip)
import bottle
bottle.debug(True)
run(reloader=True)
$ python -m bottle
Usage: bottle.py [options] package.module:app
Options:
-h, --help show this help message and exit
--version show version number.
-b ADDRESS, --bind=ADDRESS
bind socket to ADDRESS.
-s SERVER, --server=SERVER
use SERVER as backend.
-p PLUGIN, --plugin=PLUGIN
install additional plugin/s.
--debug start server in debug mode.
--reload auto-reload on file changes
Grab the 'app' object from the 'myapp.controller' module and start a paste server on port 80 on all interfaces.
python -m bottle -server paste -bind 0.0.0.0:80 myapp.controller:app
Start a self-reloading development server and serve the global default application. The routes are defined in 'test.py'
python -m bottle --debug --reload test
Install a custom debug plugin with some parameters
python -m bottle --debug --reload --plugin 'utils:DebugPlugin(exc=True)'' test
Serve an application that is created with 'myapp.controller.make_app()' on demand.
python -m bottle 'myapp.controller:make_app()''