分享

Python的web框架-Bottle

 微笑如酒 2017-12-18


Python常见的文本框架


Django

Pylons

Tornado

web.py

Flask

Bottle

这些框架我们不作详细介绍,感兴趣的同学去网站看下文档。



简单的使用Bottle开发


前后端交互的案例当中,最常见的就是表单的提交,我们以登陆验证为例,简单的介绍Bottle的使用,废话不多说,直接上代码。

  1. #!python

  2. #coding:utf-8

  3. #author:kim

  4. #copyrights 2017 www.lowpitch.cn all rights reserved.


  5. from bottle import get, post, run, request, route

  6. import MySQLdb

  7. import hashlib

  8. @route('/')

  9. @get('/login') # or @route('/login')

  10. def login():

  11.    return '''

  12.        

  13.            Username:

  14.            Password:

  15.            

  16.        

  17.    '''

  18. @post('/login') # or @route('/login', method='POST')

  19. def do_login():

  20.    username = request.forms.get('username')

  21.    password = request.forms.get('password')

  22.    if check_login(username, password):

  23.        return '

    Your login information was correct.

    '

  24.    else:

  25.        return '

    Login failed.

    '

  26. def check_login(username,password):

  27.    db = MySQLdb.connect('localhost','root')

  28.    cursor =db.cursor()

  29.    cursor.execute('use test;')

  30.    cursor.execute('select count(*) from usr where usr='%s' and psw='%s' ' \


  31. %(username,hashlib.md5(password.encode('utf-8')).hexdigest()))

  32.    status= cursor.fetchone()

  33.    db.close()

  34.    return status

  35. run(host='localhost', port=8080)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多