Search code examples
pythonweb2py

In Python application, can't find where a item comes from


I'm trying to understand how web the framework web2py works, and there is this item called request.client in access.py that I can't figure out where it comes from. I retrieved the code from here and it begins as follows:

import base64
import os
import time
from gluon.admin import apath
from gluon.fileutils import read_file
from gluon.utils import web2py_uuid
from pydal.contrib import portalocker
# ###########################################################
# ## make sure administrator is on localhost or https
# ###########################################################


http_host = request.env.http_host.split(':')[0]

My question is: when we don't know from where an item comes in a code, what is the method to find it out ?


Solution

  • As explained here, web2py model, controller, and view files are executed by the framework in an environment that has already been populated with the core API objects, include request, response, session, cache, DAL, Field, HTML helpers, and form validators (other parts of the API, such Auth, Mail, Services, Scheduler, and many additional tools and contributed libraries, are contained in modules and imported as usual).

    Specifically, the request object is created (upon each request) inside the main WSGI application (gluon.main.wsgibase) at this line, and it is subsequently passed to gluon.main.serve_controller at this line. The serve_controller function adds the request object to the execution environment and then executes model, controller, and view files in that environment.

    In addition to model, controller, and view files, your application can include its own modules. Modules in applications are not executed by the framework (they must be imported) -- therefore, those modules must access the web2py core API objects via imports from gluon (the request-dependent objects, such as request and response, are accessed via the current thread-local object, as explained here).

    It may also be helpful to review the Workflow, Libraries, and Execution environment sections of the documentation.