Python web service learning-uWSGI, pythonuwsgi

Source: Internet
Author: User

Python web service learning-uWSGI, pythonuwsgi

Recently, due to work needs, we need to use python to deploy some web Services. Because we had little knowledge about python before, it took a lot of time to learn. Here, record your learning process for future query.

As recommended by our predecessors, we first learned uWSGI.

At the beginning, you only need to understand that uWSGI is a Web server that implements the WSGI, uwsgi, http, and other protocols. For details about those protocols with similar names, I will study it later.

Well, the first step is to install uWSGI, which is very simple. Install it directly using pip.

 

Then, we can start to write our first web service: Hello World! . Create a python file as follows:

#! /Usr/bin/pythonimport osimport sysdef application (environ, start_response): status = '000000' output = 'Hello World! 'Response _ headers = [('content-type', 'text/plain '), ('content-length', str (len (output)] start_response (status, response_headers) return [output]

For the simplest uWSGI program, you only need to implement a function named application. This function has two parameters: environ is an environment variable containing http requests, and start_response is a function, set the http header. In this function, we only need to call the start_response function once, set the HTTP return header, and return an HTTP body.

Finally, start and run our applet to check the results. uwsgi -- http 172.31.11.141: 10092 -- file test. py

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.