Bottle

作者: whenitsallover | 来源:发表于2018-04-13 16:45 被阅读0次

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)


  • Routing: Requests to function-call mapping with support for clean and dynamic URLs.
  • Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
  • Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
  • Server: Built-in HTTP development server and support for paste, fapws3, bjoern, gae, cherrypy or any other WSGI capable HTTP server.

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)

相关文章

网友评论

    本文标题:Bottle

    本文链接:https://www.haomeiwen.com/subject/uzxhkftx.html