I'm a beginner and am wondering why we use self.response.out.write instead of print, and why we use classes, instead of functions, for the request handlers in the first place. Are there any special reasons?
Why does GAE use classes and self.response.out.write over functions and print?
777 views Asked by Han At
1
There are 1 answers
Related Questions in PYTHON
- How to store a date/time in sqlite (or something similar to a date)
- Instagrapi recently showing HTTPError and UnknownError
- How to Retrieve Data from an MySQL Database and Display it in a GUI?
- How to create a regular expression to partition a string that terminates in either ": 45" or ",", without the ": "
- Python Geopandas unable to convert latitude longitude to points
- Influence of Unused FFN on Model Accuracy in PyTorch
- Seeking Python Libraries for Removing Extraneous Characters and Spaces in Text
- Writes to child subprocess.Popen.stdin don't work from within process group?
- Conda has two different python binarys (python and python3) with the same version for a single environment. Why?
- Problem with add new attribute in table with BOTO3 on python
- Can't install packages in python conda environment
- Setting diagonal of a matrix to zero
- List of numbers converted to list of strings to iterate over it. But receiving TypeError messages
- Basic Python Question: Shortening If Statements
- Python and regex, can't understand why some words are left out of the match
Related Questions in GOOGLE-APP-ENGINE
- Deployment through app engine, cloud sql database, problem connecting with server code, doesn't connect
- Jetty 12 idle timeout limited to 30 seconds when upgrading to Java 21 in Google App Engine Standard Environment
- Unable to deploy to GAE from Github Actions
- Migrating Google App Engine - Eclipse Java 8
- How to use Oauth in order to log‑in on .googleapis.com on almost any arbitrary endpoints domains from the web browser?
- Running gcloud app deploy and getting PERMISSION_DENIED 'compute.regions.get', despite having Owner and Compute admin permissions
- Spring security CORS configuration not working after deploying to App Engine
- Google App Engine: manual scaling with one instance suddenly created multiple instances
- gcloud.app.deploy Error Response: [13] Failed to create cloud build: invalid bucket
- gcloud.app.deploy Error Response: [13] default Cloud Build service account or user-specified service account does not have access to the bucket
- GCP Java dev_appserver Can't make API call memcache.Get in a thread that is neither the original request thread nor a thread created by ThreadManager
- How to correlate and aggregate logs in Google App Engine Python 3?
- App Engine Python3 db / ndb & eventual consistency
- How do I connect to Google Cloud mySQL from App Engine using Django-Environ?
- How to deploy my fast api with llama 2 on app engine
Related Questions in WEBAPP2
- ssh to private IP Azure VM from local bash or Azure CLI
- HTML table not populating
- My plotly-dash app works perfectly in my local system but does not works in cyclic.sh
- Android & Webapp, call background refresh when mobile is locked
- external URL not working for streamlit app deployed using docker
- how to deploy large sized deep learning model using streamlit?
- SyntaxError: multiple exception types must be parenthesized
- has webapp2 been deprecated in app engine?
- How to use Web App approach when having functions run by editors on protected ranges, using Google Apps Scritps?
- Google Apps Script require login for webapp url / path
- Azure WebApp and docker-compose in Linux
- How to pass variable to Django Paginator()'s per_page parameter so all album tracks can be displayed on one page?
- Eclipse proposing me Webapp folder only, rather than webContent folder when creating an new project
- Google sheet + Web app search specific column and return all matching rows
- Data is received at local environment but not in production in meteor js webapp
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Using Methods
Each handler class has methods with names like
getandpost, after the HTTP methodsGETandPOSTetc. Those methods are functions that handle requests.Each request to your server will be routed to a request handler object, which is a new instance of some request handler class. So, a request handler instance is created per request, and is garbage collected once its HTTP response is sent.
By inheriting from
webapp2.RequestHandler, your handler classes get a bunch of functionality out the box for free. For example, handler instances will have the data from the HTTP request parsed into dictionaries and bound toselfasself.request.headersandself.request.bodyautomatically.The
webapp2.RequestHandlerclass also providesself.response, which is what you write your response data to.Once the new request handler instance is initialised, the inherited
__init__method calls the method that maps to the HTTP request method, so assuming aGETrequest, it callsself.get. Thewebapp2.RequestHandlerclass doesn't implement those methods; your derived class does.Responding
Neither
printnor the return value of the handler method are used here. You do not 'return a response' with this framework; you write the response to the request handler instance's (inherited)self.responseproperty.Your instance inherits
self.response.out.write(which is aliased toself.response.write), which concatenates its argument to the body of the response, initially an empty string.Note: You can call
self.response.clearto clear the response body.When you return from your handler method -
getorpostetc. - the return value is ignored. The framework uses the state ofself.responseto automatically create and send a HTTP response for you.There's a bunch of subtleties that the framework takes care of behind the scenes too.
Classes Over Functions
The main advantage is in inheritance. Normally, you'll create a single
BaseHandlerclass that derives fromwebapp2.RequestHandler. TheBaseHandlerclass will contain the core functionality for your actual handlers. It might include some logic for converting data into little JSON packages for a Web API, for example. All of the classes that actually handle requests would then be derived from yourBaseHandler.You want a custom base class for your handler classes to derive from mainly so you can edit that base class. You want that base class to inherit from
webapp2.RequestHandlerso that all your handler instances inherit the framework magic.There is enough slight of hand to make the whole thing confusing, but it is easy to make sense of once you get it, and does save a lot of trouble.
Technically, you could achieve all of the above just using functions and dictionaries, but Python is classically object oriented, so it would be painful and weird.