Introduction 🎯
Web2py is a full stack database-driven web development framework designed in python. It is an open-source framework. It supports python2 and python3. This framework has all the components to develop a complete web application.

Dispatching in Web2py is a whole process of sending and receiving data and files between client and server using the framework. This article will discuss Dispatching in Web2py, its working, and its commands.
Dispatching 🌐
Dispatching is the process of transferring a request sent by the user, and the server reverts the response. During the process of dispatching, Web2py generates a URL in the given below format:
http://127.0.0.1:8000/app/cntrl/fun.html
Concerning the function "fun" in the controller "cntrl.py" in the web application "app." If function "fun" is unavailable, then Web2py will shift to the default controller function "index." If controller "cntrl" is unavailable, then Web2py will change to the "default.py" controller. If application "app" is unavailable, then Web2py will shift to the "init" application. If application "init" is unavailable, then Web2py will change to the "welcome" application. The given diagram below shows the dispatching in Web2py:

Any new request in dispatching creates a session by default. So to track the session, a session cookie is transferred to the client browser.
The ".html" is not mandatory. It is a default extension for transferring files from the server to the client machine. The HTML extension finds the extension of the view that represents the output of the controller function "fun()," which allows the representation of the same document in different formats.
Functions having arguments or starting with a double underscore can only be called by other application functions and can not be exposed publicly.
Below given is the exceptional format made by Web2py for URLs:
http://127.0.0.1:8000/app/static/myfile
There is no controller in the Web2py application called "static." The web2py interprets the above-given URL as a request to access the file named "myfile," which is available in the subfolder "static" of the web application "app."
Web2py does not run any session or create any cookie or execute any models. It always transfers static files in chunks of 1MB and sends only that content that lies in between the range in which the client is looking for the subset of the file.
Web2py framework also supports the IF_MODIFIED_SINCE protocol, which does not send the same file which is already stored in the browser's cache.
If we add "?attachment" to the URL, it will force the browser to download the media content linked in the static folder instead of streaming it through the media player. This informs web2py to set the "Content-Disposition header "of the HTTP response to "attachment."
For Example:
<a href="/app/static/mymediafile.mp3?attachment">Download song</a>
When a user clicks on the above-given link, the browser will force the user to download the media file instead of streaming the file immediately.
Web2py creates the GET/POST request URL in the below format.
http://127.0.0.1:8000/app/cntrl/fun.html/coding/ninjas/blogs?x=5&y=10
Where "fun" is the function in controller "cntrl.py" of the application "app." It saves the URL parameters in the variable named "request," as given below:
request.args=[ ”coding”, “ninjas”, “blogs”]
and:
request.vars={“x”: 5, “y”: 10 }
and:
request.application = “app”
request.controller = “cntrl”
request.function = “fun”
“request.args[i]” and “request.args(i)” can be used to find the ith element of the “request.args” list. But if it doesn't have an index like in the above-given examples, then it will return "None" in that case.
Commands of Dispatching in Web2py
These are some common commands used while Dispatching in Web2py:
request.url: This command stores the URL of the currently executing request.
request. ajax: This command is an Ajax request. The output of this command is "false" by default, and it returns "true" only when the Web2py recognizes that the action is called by an AJAX request.
request.cid: If it is found that the request is an Ajax request and it is started by a web2py component, then the name of that component can be found by this command
If the method of an HTTP request is GET, then "request.env.request_method" is set to GET method.
If the method of an HTTP request is POST, then "request.env.request_method" is set to POST method.
request.get_vars: This command stores the variable of the URL query.
request.post_vars: This command stores all the parameters passed through the request (usually a PUT, POST, or a DELETE ).
The Storage dictionary of "request. vars" stores both the methods (get_vars and post_vars get merged).
Web2py keeps Web2py and WSGI environment variables in "request.env." For Example:
“request.env.path_info = 'app/cntrl/fun'”
Web2py stores headers of environment variables.
For Example:
“request.env.http_host = '127.0.0.1:8000'”
NOTE: To prevent directory traversal attacks, Web2py validates all the variables.
Process of Dispatching in Web2py ✉️
In this topic, we will learn how Dispatching in Web2py works for different file types.
Process of dispatching static files from the server to the client
In Web2py, URLs are restricted to contain alphanumeric characters, slashes, and underscores. The args may contain dots also before underscores restore validation spaces. Web2py returns an error message "HTTP 400" if the URL syntax is invalid. If the URL requests for a static file, Web2py executes the request and returns the requested file.
Process of Dispatching Dynamic files in Web2py
If the request of URL is not for a static file, then Dispatching in Web2py works in a different way. Given below is the handling process for dynamic file requests.
- Firstly Web2py parses the cookies.
- Then creates an environment to execute the function.
- After creating the environment Web2py initializes "request", "response", "cache".
- The next step of dispatching in Web2py is opening the existing "session" or creating a new one.
- After that, Web2py executes the models related to the received request.
- Now, after executing the models, Web2py executes the requested controller function.
- If the function returns a dictionary, Web2py runs the associated view.
- After the success of the operations, Web2py commits all the transactions.
- At last, it saves the session and returns the requested file.
NOTE: The controller and the view execute in different parts of the same environment; therefore, the "view" does not have any connection with the "controller," but it has access to see the models, and it can also see the variables returned by the controller action function.
Exception Handling during Dispatching in Web2py
Steps performed by Web2py, If an exception (other than HTTP) is raised during the dispatching process:
- Web2py stores the traceback in an error file and assigns a ticket number to that error.
- After mentioning the traceback in the error file, Web2py rollbacks all the open database transactions.
- At last, it returns an error webpage reporting the ticket.
HTTP exception is assumed to be the intended behavior. So, in this case, all the open database transactions get committed without any problem. The HTTP itself describes the intentional behavior after saving all the transactions. The Web2py framework defines the HTTP exception class and is not a python exception class.
Frequently Asked Questions
What is RBAC in Web2py?
RBAC is a compelling Role Based Access Control system that restricts authorized users' access.
How can we delete the records from the database through Web2py?
Users can delete the record saved in the server by the following command:
“curd.delete(db.table,id)”
What is an HTTP request?
An HTTP request is a client request to the server through the domain to receive some HTML document.
What is an HTTP response?
The HTTP response is a transfer of an HTML page by the server to the client against the request made by the client.
What is Web Cache?
Web Cache is a storage system that stores images and data in the browser so that if the user wants to search the same data again, then it will not take time to show it again.




