Last active
April 29, 2016 15:53
Star
You must be signed in to star a gist
Python初心者がBottleを使ってみたら案外簡単に動いてくれた ref: http://qiita.com/masaibar/items/e3b6911aee6741037549
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#importするためにpip installしておく | |
$ pip install bottle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from bottle import route, run | |
@route('/hello') | |
def hello(): | |
return "Hello World!" | |
run(host='localhost', port=8080, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from bottle import route, run | |
@route('/hello/') | |
@route('/hello/<user>') | |
def hello(user="taro"): | |
return "Hello {user}".format(user=user) | |
@route('/date/<month:re:[a-z]+>/<day:int>/<path:path>') | |
def date(month, day, path): | |
return "{month}/{day} {path}".format(month=month, day=day, path=path) | |
run(host='localhost', port=8080, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from bottle import route, run | |
from bottle import get, post, request | |
@route('/login', method='GET') # or @get('/login') | |
def login(): | |
username = request.query.get('user') | |
password = request.query.get('pass') | |
#GETで何も渡されていない時はusername,passwordに何も入れない | |
username = "" if username is None else username | |
password = "" if password is None else password | |
return ''' | |
<form action="/login" method="post"> | |
Username: <input name="username" type="text" value="{username}"/> | |
Password: <input name="password" type="password" value="{password}"/> | |
<input value="Login" type="submit" /> | |
</form> | |
'''.format(username=username, password=password) | |
@route('/login', method='POST') # or @post('/post') | |
def do_login(): | |
username = request.forms.get('username') | |
password = request.forms.get('password') | |
return "{username} {password}".format(username=username, password=password) | |
run(host='localhost', port=8080, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from bottle import route, run | |
from bottle import error | |
@route('/hello') | |
def hello(): | |
return "Hello World!" | |
@error(404) | |
def error404(error): | |
return "Nothing here sorry {error}".format(error=error) | |
run(host='localhost', port=8080, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment