Python development of web service bottle framework

Development functions are not particularly complex web services, you can consider using the bottle framework. Reasons: 1. Python development efficiency is high! Do not believe you Bibi can get the same function in a few lines of Python? Try changing to java? Try changing to C++? As a person who has used all these languages, I try not to use C++ when I have used Java, and I try not to use Java when I have used Python. It is really unbearable to look back.

Use the bottle frame to install first. Get it done with one instruction.


# pip install bottle

To share a pip problem encountered, my Python version is very low 2.6.6. Originally by installing epel of yum, the useful pip has been successfully installed. But every time I use the pip command, there will be a prompt to upgrade at the bottom



You are using pip version 9.0.3, however version 20.0.2 is available.You should consider upgrading via the 'pip install --upgrade pip' command


I saw that this prompt was not malicious, so I executed pip install --upgrade pip. Then the pip command can no longer be used. It is an effect that the shell command ls cd cannot be executed after the error of upgrading glibc. And after the upgrade, there is no corresponding installation package when you want to install back to the lower version of pip. Yum can only find the 20.0.2 version (will the old version be overwritten?).


http://bootstrap.pypa.io/2.6/get-pip.py

After downloading get-pip.py, it cannot be installed successfully. Finally, I downloaded the 2.6 version of the installation file at the above address, and then successfully installed the pip that can be used. (Do you see 2.6 in the address?)

After pip install bottle is successful, enter the Python command line import bottle. If no error is reported, it is successful. My web service is a file bottleweb.py, the code is as follows


#coding=utf-8from bottle import (run, route, get, post, put, delete, request, hook, response, static_file, app)import jsonimport MySQLdb #本例子需要操作数据库,否则可以不写这行,这个数据库包pip估计安装不会成功,我是用yum install MySQL-python成功的import sysreload(sys)sys.setdefaultencoding('utf8')
import bottleapp = bottle.default_app()#处理静态资源需要定义,没有静态资源可以不写这行#搭建vue脚手架前后台联调时要下面两个@hook内容,否则会报跨域访问资源的错误@hook('before_request')def validate():    REQUEST_METHOD = request.environ.get('REQUEST_METHOD')
   HTTP_ACCESS_CONTROL_REQUEST_METHOD = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_METHOD')    if REQUEST_METHOD == 'OPTIONS' and HTTP_ACCESS_CONTROL_REQUEST_METHOD:        request.environ['REQUEST_METHOD'] = HTTP_ACCESS_CONTROL_REQUEST_METHOD

@hook('after_request')def enable_cors():    response.headers['Access-Control-Allow-Origin'] = '*'    response.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,DELETE,OPTIONS'    response.headers['Access-Control-Allow-Headers'] = '*'
@route('/test2020/dist/<path>')#静态资源在web服务下的地址,没放前端的静态资源这几个route和app.route可以不写def stat(path):    return static_file(path, root='./dist/')
@app.route('/test2020/dist/static/js/<path>')  def js(path):  #这几个目录我写成这样是因为vue打包完后目录结构就是dist 里面static等等    return static_file(path, root='./dist/static/js/')
@app.route('/test2020/dist/static/css/<path>')  def css(path):      return static_file(path, root='./dist/static/css/') @get('/test2020/date')#返回某个表中的日期,看sql你就明白了def helloins():    db = MySQLdb.connect("127.0.0.1", "yourusername", "yourpassword", "yourDBname", charset='utf8' )    cursor = db.cursor()    sql = "select DISTINCT date from testtable"    print sql    cursor.execute(sql)    data = cursor.fetchall()    jsondata={}    results=[]    for row in data:        result = {}        result['DATE'] = row[0]        results.append(result)    jsondata['code']=0    jsondata['datas']=results    return jsondata  #返回json格式为了方便前端vue接收处理,其实返回各种类型都可以    @get('/test2020/helloworld')def helloworld():    return 'hello world!'    if __name__ == '__main__':    run(host='0.0.0.0', port=2020, debug=True, reloader=True)

Execute #python bottleweb.py in the directory where bottleweb.py is located, and the web service is easy to start, right? Browser visit http://127.0.0.1:2020/test2020/helloworld to try

If you have installed the MySQL database, you can test whether the url of test2020/date can return results

The database only has the following data

image

The front-end page looks like this, for the user to select a certain date for the mobile terminal.

image

The front end is developed with vue+vux, and the packaged result is the things in the dist directory mentioned above. This article will not discuss it in detail. Later, I will talk about some pitfalls in MySQL and Vue development. If you think the above code is a bit complicated, you can delete all the route, app.route stuff, delete the /test2020/date statement block, delete @hook, delete MySQL stuff, and ignore the front-end stuff at all. Simple bottle web service. This helps to learn step by step. If it helps you, please help me.


Guess you like

Origin blog.51cto.com/15080029/2642973