我爱编程

Bottle

2018-04-13  本文已影响0人  whenitsallover

http://bottlepy.org/docs/dev/
Bottle 是Python微型框架中的微型框架
一个py文件就可运行, 6行代码 打印‘hello world’

Hello world

from bottle import route, run
@route('/hello')
def hello():
    return "Hello World!"
if __name__ == '__main__':
    run(host='localhost', port=8080)


Dynamic Route

@route('/article/<id>')
def article(id):
    return '<h1> You are viewing article' + id + ' </h1>'

@route('/page/<id>/<name>')
def views(id,name):
    return 'You are viewing the page '+ id  + ' with the name of ' + name

simple login html


from bottle import route, run, template,request,post
@route('/login')
def login():
    if request.method == "GET":
        return '''
        <form action="/login" method="post">
            Username: <input name="username" type="text" />
            Password: <input name="password" type="password" />
            <input value="Login" type="submit" />
        </form>
    '''
@post('/login') # or @route('/login', method='POST')
def do_login():
    username = request.forms.get('username')
    password = request.forms.get('password')
    if username == 'parker' and password == '123':
        return "<p>Your login information was correct.</p>"
    else:
        return "<p>Login failed.</p>"

run(host='localhost', port=8080)
上一篇下一篇

猜你喜欢

热点阅读