In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use Pyramid and Cornice to write Python Web API", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write Python Web API using Pyramid and Cornice.
Establish Pyramid application
First create a virtual environment for your application and create a file to save the code:
$mkdir tutorial$ cd tutorial$ touch main.py$ python3-m venv env$ source env/bin/activate (env) $pip3 install cornice twisted imports Cornice and Pyramid modules
Use the following command to import these modules:
From pyramid.config import Configuratorfrom cornice import Service definition service
Define the reference service as a Service object:
QUOTES = Service (name='quotes', path='/', description='Get quotes') write reference logic
So far, this only supports getting famous quotes. Decorate the function with QUOTES.get. This is the way to bind logic to a REST service:
QUOTES.get () def get_quote (request): return {'William Shakespeare': {' quote': ['Love all, trust a few, do wrong to none',' Some are born great, some achieve greatness, and some have greatness thrust upon them.']}, 'Linus': {' quote': ['Talk is cheap. Show me the code.']}}
Note that unlike other frames, the decorator does not change the get_quote function. If you import this module, you can still call the function periodically and check the results.
This is useful when writing unit tests for Pyramid RESTful services.
Define application object
Finally, use scan to find all the decorated functions and add them to the configuration:
With Configurator () as config: config.include ("cornice") config.scan () application = config.make_wsgi_app ()
Scan the current module by default. If you want to scan all the modules in the package, you can also provide the name of the package.
Run the service
I use Twisted's WSGI server to run the application, but if necessary, you can use any other WSGI server, such as Gunicorn or uWSGI.
(env) $python-m twisted web-- wsgi=main.application
By default, Twisted's WSGI server runs on port 8080. You can use HTTPie to test the service:
(env) $pip install httpie... (env) $http GET HTTP/1.1 200 OKContent-Length: 220Content-Type: application/jsonDate: Mon, 02 Dec 2019 16:49:27 GMTServer: TwistedWeb/19.10.0X-Content-Type-Options: nosniff {"Linus": {"quote": ["Talk is cheap. Show me the code. "]}," William Shakespeare ": {" quote ": [" Love all,trust a few,do wrong to none "," Some are born great, some achieve greatness, and some greatness thrust upon them. "]}} Why should I use Pyramid?
Pyramid is not the most popular framework, but it has been used in some high-profile projects such as PyPI. I like Pyramid because it is one of the frameworks that take unit testing seriously: because the decorator does not modify functions and has no thread local variables, functions can be called directly from unit tests. For example, a function that needs to access a database will get it from a request.config object passed through request.config. This allows unit testers to put simulated (or real) database objects into requests without having to carefully set global variables, thread-local variables, or other framework-specific things.
If you are looking for a tested library to build your next API, try using Pyramid. You won't be disappointed.
At this point, I believe you have a deeper understanding of "how to write Python Web API with Pyramid and Cornice". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.