Learn python:wsgi_02_ use paste Library __python

Source: Internet
Author: User

In the previous article we learned how a simple applet works, and here we'll go on with the Paste library in Python.

In the previous applet, the request was for only the "/" path, only one application (although theoretically you could do a lot of things in this application), what if we wanted different applications to handle different request paths?

A common solution is that we can easily use the paste library to achieve our ideas.

First look at the example, the file is example02.py, the code reads as follows:

Import OS from Paste.deploy import Loadapp from wsgiref.simple_server Import Make_server class Homeshow (object): Def

    __init__ (self): print ("init homeshow.") def __call__ (self, Environ, start_response): Status_code = "OK" response_headers = [("Content-type", "

        Text/plain ")] response_body =" Homeshow ' s response body. " Start_response (Status_code, response_headers) return [response_body] @classmethod def factory (CLS, Global
        _conf, **kwargs): Print ("Homeshow factory.")

    return Homeshow () class Volumeshow (object): Def __init__ (self): print ("init volumeshow.") def __call__ (self, Environ, start_response): Status_code = "OK" response_headers = [("Content-type", "

        Text/plain ")] response_body =" Volumeshow ' s response body. " Start_response (Status_code, response_headers) return [response_body] @classmethod def factory (CLS, Global _conf, **kwargs): Print ("Volumeshow factory.") Return Volumeshow () if __name__ = = "__main__": configfile = "Paste.ini" Application_name = "Demo" Application s = Loadapp ("config:%s"% Os.path.abspath (configfile), application_name) server = Make_server ("localhost", 8052, Appli
 cations) Server.serve_forever ()

The corresponding configuration file Paste.ini the following contents:

[Composite:demo]
Use=egg:paste#urlmap
/:homeshow
/volume:volumeshow

[app:homeshow]
paste.app_factory = example02: Homeshow.factory

[app:volumeshow]
paste.app_factory = example02:VolumeShow.factory

In the corresponding file directory, execute the command python example02.py

Normally, you can see the factory method in each class and the print information in the initialization method in the Command line window.

Open the browser, enter: http://localhost:8052, and then enter, you can see the URL to/response:

Replace the URL content, enter: Http://localhost:8052/volume, and then enter, you can see the URL for the/volume response:

OK, which means that the code works correctly.

First we look at the configuration file inside, it's very simple, it uses the URL mapping method in paste to distribute the request. For different URLs, a different app is configured to handle it.

To get back to the code, look at the bottom part: Specify the configuration file to use, find the Application "collection" (here corresponds to the name "demo" in the [Composite:demo] section of the configuration), and take advantage of the Loadapp in Paste.deploy () Build the Application collection object to deploy, deploy the application collection to the server, and invoke the corresponding method so that the service can always respond to the request;

Back to the beginning of the code, it actually defines two application corresponding classes. It is to be noted that the object is generated by the factory method in the respective class, and the response is finally completed. This is also reflected in the configuration.

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.