web.py Framework study notes-CTX

Source: Internet
Author: User
Tags response code

Summary:

CTX is used to access the environment variables for Web requests, and is instantiated based on the Threadeddict class. The Threadeddict class instantiates an object of the dictionary type that some properties can use to access the ID of the processing thread. This way, the thread pool of the dictionary instance makes the concurrency control of Web Access a good solution, and the threads are very independent.

Example:

1 class Example: 2     def GET (self): 3         Referer = Web.ctx.env.get ('http_referer'http://google.com ' )4         raise web.seeother (referer)

The above code uses WEB.CTX.ENV to get the http_referer environment variable. If there is no value, the default is to return ' http://google.com ' and redirect to the Web site.

The web.ctx can be set by the Loadhook (Mount) method. For example, whenever a service receives a Web request, the session data is stored in WEB.CTX. Because Web.ctx is thread-safe, you can use the session data at this point (the data must be a recognized data type in Python).

The data contained in CTX:

Environ is env A dictionary containing the format of the standard WSGI environment variable
Home The Web app's root directory, with the display name of any external app reference, for example, Http://example.org/admin
Homedomain ? (Shown as protocol + host) http://example.org
HomePath User-requested path, HomePath + Path = User's complete request address, interacting with URLs when processing
Host Host name, including port (default 8080)
Ip The user IP that originated the Web request
Method Web request method initiated
Path The user requests a part of path, for example,/logon/authenticated, but for mounted sub-apps, the name of the child app is not displayed. For example, the main app myapp.py, sub-app subapp.py. If there are page/read/blogs in the sub-application, only example.org/read/blogs will be returned in CTX instead of Example.org/subapp/read/blogs
Protocol Request to follow the protocol http/https/ftp ...
Query Conditions requested in the URL
FullPath is Path+query The complete request address, including the requested page and the requested condition

Response Status:

    • Status-http status code, (default 200,ok) exception response code: 401 Unauthorized
    • Headers-two tuples containing the request header (HTTP header) information
    • Output-A string containing the response entity

web.py Framework study notes-CTX

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.