In GAE, you can say users.get_current_user() to get the currently logged-in user implicit to the current request. This works even if multiple requests are being processed simultaneously -- the users module is somehow aware of which request the get_current_user function is being called on behalf of. I took a look into the code of the module in the development server, and it seems to be using os.environ to get the user email and other values associated to the current request.
Does this mean that every request gets an independent os.environ object?
I need to implement a service similar to users.get_current_user() that would return different values depending on the request being handled by the calling code. Assuming os.environ is the way to go, how do I know which variable names are already being used (or reserved) by GAE?
Also, is there a way to add a hook (or event handler) that gets called before every request?
As the docs say,
A Python web app interacts with the
App Engine web server using the CGI
protocol.
This basically means exactly one request is being served at one time within any given process (although, differently from real CGI, one process can be serially reused for multiple requests, one after the other, if it defines main functions in the various modules to which app.yaml dispatches). See also this page, and this one for documentation of the environment variables CGI defines and uses.
The hooks App Engine defines are around calls at the RPC layer, not the HTTP requests. To intercept each request before it gets served, you could use app.yaml to redirect all requests to a single .py file and perform your interception in that file's main function before redirecting (or, you could call your hook at the start of the main in every module you're using app.yaml to dispatch to).
Related
I read the Flask doc, it said whenever you need to access the GET variables in the URL, you can just import the request object in your current python file?
My question here is that if two user are hitting the same Flask app with the same URL and GET variable, how does Flask differentiate the request objects? Can someone tell me want is under the hood?
From the docs:
In addition to the request object there is also a second object called
session which allows you to store information specific to a user from
one request to the next. This is implemented on top of cookies for you
and signs the cookies cryptographically. What this means is that the
user could look at the contents of your cookie but not modify it,
unless they know the secret key used for signing.
Means every user is associated with a flask session object which distinguishes them from eachother.
Just wanted to highlight one more fact about the requests object.
As per the documentation, it is kind of proxy to objects that are local to a specific context.
Imagine the context being the handling thread. A request comes in and the web server decides to spawn a new thread (or something else, the underlying object is capable of dealing with concurrency systems other than threads). When Flask starts its internal request handling it figures out that the current thread is the active context and binds the current application and the WSGI environments to that context (thread). It does that in an intelligent way so that one application can invoke another application without breaking.
I would like to exploit Python Eve features, but I have a custom web environment where I have my Request objects and (can be disabled) a Router.
I know Python Eve is built on top of Flask and those features are already there, but I would like somehow to wrap/adapt my custom requests into Python Eve / Flask ones.
I have a process acting as a webserver (It receives and sends messages in protocols different from HTTP). I was searching for a standard way to interface it to Eve or Flask. I found out WSGI.
To further clarify: Imagine you have your ESB which is able to vehicle HTTP requests.
If you want to handle those requests with Eve, you should build a gateway/bridge.
It means, implementing something that:
Receives the proprietary or not standard protocol containing the request
Extracts the most important parameters from the request, such as URL, QUERY_STRING, HTTP method and so on...
Fills a WSGI Environment with those parameters following the PEP
Runs the WSGI application (in our case an Eve instance)
We get the response from the WSGI application
Pack the response back into your proprietary or custom protocol
Send back to requestor
A really simple example can be found at http://ivory.idyll.org/articles/wsgi-intro/what-is-wsgi.html
I am not sure what you mean. Do you want to use Eve with a different (custom) framework other than Flask? That's going to be really hard without an almost total rewrite, as Eve is, in fact, a Flask application (a subclass actually).
I am new to the programming world and trying out something with Python.
My requirement is to have http web server(built using BaseHTTPServer) that runs forever, which takes an input binary file through HTML form based on user selection and returns a set of HTML files back on the web client.
As part of this when the user is selecting his specific input file, there are set of folders created with HTML files written inside those folders in the server, i thought of putting in a tidy up functionality for these folders on the server. So that everyday the tidy up would clean up the folders automatically based on a configuration.
i could build both these modules in my script(http web service & tidy up on server), specifically the tidy up part is achieved using python's sched module
Both of these functionalities are working independently, i.e
when i comment out the function for tidy up, i can access the server url in the browser and the index.html page shows up correctly and further(accepts binary, parsing happens and output htmls are returned)
when i comment out the function for http server, based on the configuration set, i am able to ensure the tidy up functionality is working
But when i have both these functions in place, i see that the tidy up function works/is invoked correctly for the scheduled time, but the index.html page is not loaded when i request for the server on the browser
I researched on the sched module enough to understand that it is just to schedule multiple events on the system by setting time delays and priorities
Not able to work both the functionality
Questions:
Is this a correct approach, using sched to achieve the tidy up?
If yes, what could be the reason that the http service functionality is blocked and only the tidy up is working?
Any advice would be helpful. Thanks
For now, changed the function call for the tidy up feature, by using the background scheduler implementation of the APScheduler module of python.
This does not impact the function for serving http requests and has currently solved my problem
I am a newbie to Google App Engine and Python.
I want to create an entry in a SessionSupplemental table (Kind) anytime a new user accesses the site (regardless of what page they access initially).
How can I do this?
I can imagine that there is a list of standard event triggers in GAE; where would I find these documented? I can also imagine that there are a lot of system/application attributes; where can I find these documented and how to use them?
Thanks.
I am trying to be pretty general here as I don't know whether you are using the default users service or not and I don't know how you are uniquely linking your SessionSupplemental entities to users or whether you even have a way to identify users at this point. I am also assuming you are using some version of webapp as that is the standard request handling library on App Engine. Let me know a bit more and I can update the answer to be more specific.
Subclass the default RequestHandler in webapp with a new class (such as MyRequestHandler).
In your subclass override the initialize() method.
In your new initialize() method get the current user from your session system (or the users service or whatever you are using). Test to see if a SessionSupplemental entity already exists for this user and if not create a new one.
For all your other request handlers you now want to subclass MyRequestHandler (instead of the default RequestHandler).
Whenever a request happens webapp will automatically call the initialize() method.
This is going to cost you a read for every request and also a write for every request by a new user. If you use the ndb library (instead of db) then a lot of the requests will just hit memcache instead of the datastore.
Now if you are just starting creating a new AppEngine app I would recommend using the Python27 runtime and webapp2 and trying to leverage as much of the webapp2 Auth module as you can so you don't have to write so much session stuff yourself. Also, ndb can be much nicer than the default db library.
I'm using CherryPy to make a web-based frontend for SymPy that uses an asynchronous process library on the server side to allow for processing multiple requests at once without waiting for each one to complete. So as to allow for the frontend to function as expected, I am using one process for the entirety of each session. The client-side Javascript sends the session-id from the cookie to the server when the user submits a request, and the server-side currently uses a pair of lists, storing instances of a controller class in one and the corresponding session-id's in another, creating a new interpreter proxy and sending the input if a non-existant session-id is submitted. The only problem with this is that the proxy classes are not deleted upon the expiration of their corresponding sessions. Also, I can't see anything to retrieve the session-id for which the current request is being served.
My questions about all this are: is there any way to "connect" an arbitrary object to a CherryPy session so that it gets deleted upon session expiration, is there something I am overlooking here that would greatly simplify things, and does CherryPy's multi-threading negate the problem of synchronous reading of the stdout filehandle from the child process?
You can create your own session type, derived from CherryPy's base session. Use its clean_up method to do your cleanup.
Look at cherrypy/lib/sessions.py for details and sample session implementations.