Attributes of the Request
The Following are the attributes of the Request, some of which are instances of storage class.
-
request.cookies: The cookies sent with the HTTP request are contained in the SimpleCookie() object. It functions as a cookie dictionary. Every cookie is a Morsel.
-
request.env: a Storage object that holds the environment variables supplied to the controller, such as common WSGI arguments and HTTP header information from the HTTP request. To make it simpler, the environment variables are all changed to lower case, and the dots are changed to underscores.
-
request.application: the name of the requested application.
-
request.controller: the name of the requested controller.
-
request.function: the name of the requested function.
-
request.extension: The preset value is "html". This basically used to determine the extension of the view file that will render the dictionary (parsed from the request.env.path info) if the controller function returns a dictionary but does not specify a view.
-
request.folder: the directory of applications. For instance, request.folder is set to the absolute path "/path/to/welcome" if the application is "welcome." When creating paths to the files you need to access in your programs, you should always utilize this variable and the os.path.join function. It is a good rule to never explicitly modify the current working folder (whatever that may be), even though web2py always uses absolute paths, as this is not a thread-safe technique.
-
request.now: a datetime.datetime object containing the request's date and time.
-
request.utcnow: The current request's UTC date and time is stored in the datetime object.
-
request.args: A list of URL path components following the controller function name; equivalent to request.env.path_info.split('/')
-
request.vars: a gluon.storage.Storage object containing all the request parameters.
-
request.get_vars: a gluon.storage.Storage object containing only the parameters passed into query string (a request to /a/d/e?var1=2&var2=1 will end in {var1: "2", var2: "1"})
-
request.post_vars: a gluon.storage.Storage object containing only parameters that are passed into the request body (usually in POST, PUT, and DELETE requests).
-
request.client: The IP address of the client is determined by, if present, request.env.http_x_forwarded_for or request.env.remote_addr. Since this is useful, it should not be trusted because the http_x_forwarded_for can be copied.
-
request.is_local: True if the client is localhost, False otherwise. Should work behind a proxy if the proxy supports http_x_forwarded_for.
-
request.is_https: True if the request is using the HTTPS protocol, False otherwise.
-
request.body: a read-only file stream containing the HTTP request's body. This is automatically parsed to get the request.post_vars and then reminded. It can be read with request.body.read().
-
request.ajax is True if the function is being called via an Ajax request.
-
request.cid is the id of the component that generated the Ajax request (if any).
-
request.requires_https() prevents further code execution if the request is not over HTTPS and redirects the visitor to the current page over HTTPS.
-
request.restful this is a new and very useful decorator that can be used to change the default behavior of web2py actions by separating GET/POST/PUT/DELETE requests.
-
request.user_agent() parses the user_agent field from the client and returns the information in the form of a dictionary. It is useful to detect mobile devices. It uses "gluon/contrib/user_agent_parser.py" created by Ross Peoples. To see what it does, try to embed the following code in a view:
{{=BEAUTIFY(request.user_agent())}}
-
request.global_settings contains web2py system-wide settings. They are set automatically and you should not change them. For example request.global_settings.gluon_parent contains the full path to the web2py folder, request.global_settings.is_pypy determines if web2py is running on PyPy.
-
request.wsgi is a hook that allows you to call third-party WSGI applications from inside actions.
The latter includes:
request.wsgi.environ
request.wsgi.start_response
request.wsgi.middleware
Their application is covered in the WSGI section at the conclusion of this chapter.
Consider the following call on a typical system as an illustration:
http://127.0.0.1:8000/exam/default/status/x/y/z?p=2&q=1
results in the following request object:
{
"request.application":"examples",
"request.controller":"default",
"request.function": "status",
"request.extension":"html",
"request.view":"status",
"request.folder": "applications/examples/",
"request.args": "'x', 'y', 'z']",
"request.vars": "<Storage {'p': 1, 'q': 2}>",
"request.get_vars": "<Storage {'p': 1, 'q': 2}>",
"request.post_vars": "<Storage {}>",
"request.is_local": "False",
"request.is_https": "False",
"request.ajax": "False",
"request.cid" : "None",
"request.wsgi": "<hook>",
"request.env.content_length": "0",
"request.env.content_type": ,
"request.env.http_accept": "text/xml,text/html",
"request.env.http_accept_encoding": "gzip, deflate",
"request.env.http_accept_language": "en",
"request.env.http_cookie": "session_id_examples=127.0.0.1.119725",
"request.env.http_host":"127.0.0.1:8000",
"request.env.http_referer":"http://web2py.com/",
"request.env.http_user_agent": "Mozilla/5.0",
"request.env.path_info": "/examples/simple_examples/satus",
"request.env.query_string": "p=1&q=2",
"request.env.remote_addr":"127.0.0.1",
"request.env.request_method": "GET",
"request.env.script_name": ,
"request.env.server_name": "127.0.0.1",
"request.env.server_port": "8000",
"request.env.server_protocol": "HTTP/1.1",
"request.env.server_software": "Rocket 1.2.6",
"request.env.web2py_path": "/Users/mdipierro/web2py",
"request.env.web2py_version": "Version 2.4.1",
"request.env.wsgi_errors" : "<open file, mode 'w' at >",
"request.env.wsgi_input": ,
"request.env.wsgi_url_scheme":"http",
}
The web server determines which environment variables are defined. Here, we're assuming that the Rocket WSGI server is built-in. When utilizing the Apache web server, the list of variables is not drastically changed.
The request HTTP header is parsed to produce the request.env.http_* variables.
If your apps need to know the location, version, and whether web2py is running on the Google App Engine, web2py creates the request.env.web2py_* variables rather than parsing them from the web server environment (because specific optimizations may be necessary).
The request.env.wsgi_* variables also need to be noted. The WSGI adaptor is required for their use.
Now, let us look at some Faqs based on the above discussion:
Frequently Asked Questions
Is web2py worth learning?
Web2py is a top-notch framework, yes. The simplicity of usage, from installation to learning to code to distribution to deployment, is a key objective of web2py.
What is the use of web2py?
Python dynamic web content programming is made possible via Web2py. Web2py is made to make laborious web development jobs more manageable, such as creating web forms from scratch, while a web developer can still do it if necessary.
Is web2py an MVC model?
The Ruby on Rails and Django frameworks inspired the creation of web2py. Web2py is similar to these frameworks in that it emphasizes rapid development, prefers convention over configuration, and adheres to the model-view-controller (MVC) architectural pattern.
Does web2py support Python 3?
Web2py functions on Python 2.7 and Python 3 along with CPython (the C implementation) and PyPy (Python written in Python).
Which is better, web2py or Flask?
The majority of the Slant community suggests Flask when comparing web2py to that framework. What are the best backend web frameworks, as in the question? Web2py is ranked 19th, while Flask is placed fourth.
Conclusion
This article has an extensive understanding of requests in web2py and what are the attributes of requests in web2py.
After reading about Other types of Forms in web2py, refer to the web2py web framework,
What is the web2py web framework, web2py introduction, web2py installation, Creating a New Web2Py Application, Web2Py - Prepare the Tool, Web2Py - Troubleshooting Use Cases, and Web2Py - Capacity Planning Use Cases, Application development using Python, Introduction to Python- Coding Ninjas for a deeper understanding of web2py development and other related topics.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more!
