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