current position:Home>python wsgi

python wsgi

2022-02-01 20:05:29 waws520

wsgi

wsgi The full name is "Web Server Gateway Interfacfe",web Server gateway interface ,wsgi stay python2.5 Add , yes web The server and web Application standard interface , Any that implements the interface web The server and web Applications can work seamlessly . Look at a picture of image points :

   As shown in the figure above ,wsgi One end is connected to web The server (http The server ), Connect the application at the other end . There are already many web frame , Such as flask、webpy, Users simply configure some routing information , You can develop one web application . In the following text web Framework is equivalent to application (application)

   So what does this interface look like ,pep333 That's how it's described :

  (1)App Provide a that accepts two parameters callable object . The first parameter is a dict type , Represents the environment variable associated with the request ; The second parameter is a callable object ,app Call this in code callable Set the message header of the response .app You need to return an iteratable string sequence , Indicates the message content of the response

  (2)web server stay http When the request comes , call App Provided callable object , The parameters of the incoming response . And then put App Send the return value of to http Requestor ( Such as browser )

First , Let's look at the most basic example ,wsgi Of Hello World!

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')] # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return ["Hello World"]
 Copy code 

Of course , We need to web The server can test this App,python Provides a pure python It's simple web The server :wsgiref. This implementation is single threaded , The performance is not very good , But it can be used for testing .

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')] # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return ["Hello World"]

def main():
    from wsgiref.simple_server import make_server
    httpd = make_server('', 8000, hello_world_app)
    print "Serving on port 8000..."

    # Serve until process is killed
    httpd.serve_forever()

if __name__ == '__main__':
    main()
 Copy code 

Run this code , Then type in the browser 127.0.0.1:8000, You can see the result

common python web The server includes ,cherrypygevent-fastcgigunicornuwsgitwistedwebpytornado wait , in application , these web The server may need to cooperate with other servers http Proxy use , Such as Nginx.

common python web The framework includes Django, Flask, webpy,bottle,Cherrypy,tornado. some web The framework itself comes with web The server , Such as Cherrypy、tornado.tornado Because there are asynchronous network libraries , As web The server performance is also good .

   Last , Recommend an article Let’s Build A Web Server. The article consists of three parts : Part I Introduction http service ; The second part implements a wsgi web The server , And cooperate with various web The framework is used and tested ; The third part realizes the second part wsgi The server is optimized for concurrency .

copyright notice
author[waws520],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202012005199227.html

Random recommended