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