In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "Python development web service method is what", in the actual case of the operation process, many people will encounter such a dilemma, then let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Developing web services with functionality that is not particularly complex, consider using a bottle framework. Reason: Python development efficiency is high! Do not believe you compare the same function Python a few lines can be done? Try Java? Try C++ instead? As I have used these languages, I try not to use C++ when I have used Java, and try not to use Java when I have used Python. It is really unbearable to look back.
Use bottle frame to install first. One command.
# pip install bottle
Share a pip problem encountered, my Python version is very low 2.6.6. Originally installed through yum epel, has successfully installed a useful pip. But every time I use pip command, the bottom prompt will upgrade
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 see no malice in this prompt, so I execute pip install --upgrade pip. Then pip command can not be used, and after upgrading glibc error, ls cd shell command can not be executed is an effect. And after upgrading, there is no corresponding installation package when you want to install back to the lower version of pip. Yum can only find version 20.0.2 (will older versions be overwritten?).
http://bootstrap.pypa.io/2.6/get-pip.py
Download get-pip.py also failed to install successfully. Finally, download the installation file of version 2.6 at the above address, and successfully install the pip that can be used. (See 2.6 in the address?)
After pip install bottle succeeds, enter Python command line import bottle, no error is successful. My web service is a file named bottleweb.py, and the code is as follows
#coding= utf-8 from bottle import (run, route, get, post, put, delete, request, hook, response, static_file, app)import jsonimport MySQLdb #This example requires database operation, otherwise you can omit this line, this database package pip estimated installation will not succeed, I use yum install MySQL-python successful import sysreload(sys)sys.setdefaultencoding ('utf8 ')
import bottleapp = bottle.default_app()#Static resources need to be defined. If there are no static resources, you can omit this line #When building vue scaffolding, you need the following two @hook contents, otherwise you will report an error of cross-domain access to resources @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/')#Address of static resources under web service, static resources without front-end are not required to write def stat(path) for these routes and app. route: return static_file(path, root='./ dist/')
@app.route ('/test2020/dist/static/js/') def js(path): #I wrote these directories like this because after vue is packaged, the directory structure is static in dist, etc. return static_file(path, root='./ dist/static/js/')
@app.route('/test2020/dist/static/css/') def css(path): return static_file(path, root='./ dist/static/css/') @get ('/test2020/date')#Returns a date in a table, see sql you understand 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 #return json format In order to facilitate front-end vue receiving processing, in fact, return various types can be @get('/test2020/helloworld')def helloworld(): return 'hello world! ' if __name__ == '__main__': run(host='0.0.0.0', port=2020, debug=True, reloader=True)
Bottleweb.py is in the directory where #python bottleweb.py is executed, and the web service starts. Simple, right? Browser Visit http://127.0.0.1:2020/test2020/helloworld Try
If you have MySQL installed, you can test whether the url of test2020/date returns results.
The database only has the following data
The front-end page looks like this, giving the user to select a certain date for the mobile phone.
The front-end is developed with vue+vux, and the packaged results are the things under the dist directory mentioned above. This article does not discuss in detail. I'm going to talk about some of the pitfalls of MySQL and vue development later. If you think the above code is a bit complicated, you can delete all the route, app.route things, delete the/test2020/date statement block, delete the @hook, delete MySQL things, and completely ignore the front-end things. It is the simplest bottle web service. This helps you learn step by step. If it helps you, please help me look at it.
"Python web service development method is what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.