Skip to content

Instantly share code, notes, and snippets.

@hideshi
Last active January 1, 2016 14:09
Show Gist options
  • Save hideshi/8155642 to your computer and use it in GitHub Desktop.
Save hideshi/8155642 to your computer and use it in GitHub Desktop.
This test drive is made by Bottle which is the light weight web application framework for Python.
from bottle import view, route, get, post, request, redirect, template, run
@route('/')
@view('index')
def index():
return dict()
@route('/hello/<name>')
def index(name='World'):
return template('Hello {{name}}!', name=name)
@route('/<name>/<age:int>')
def greet(name, age):
return template('Hello {{name}}. You are {{age}} years old!', name=name, age=age)
@route('/wrong')
def wrong():
return redirect('/right')
@route('/right')
def right():
return 'right'
@get('/login') # or @route('/login')
def login_form():
return '''<form method="POST" action="/login">
<input name="name" type="text" />
<input name="password" type="password" />
<input type="submit" />
</form>'''
@post('/login') # or @route('/login', method='POST')
def login_submit():
name = request.forms.get('name')
password = request.forms.get('password')
if check_login(name, password):
return "<p>Logged in</p>"
else:
return "<p>Login failed</p>"
def check_login(name, password):
if name == password:
return True
else:
return False
run(host='localhost', port=8080, debug=True, reloader=True)
<html>
<title>Test Page</title>
<body>
<h1 align="center">Hello world!</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment