Bottle

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容