What's the difference between application and request contexts? - flask

Flask documentation says that there are 2 local context: application context, and request context. Both are created on request and torn down when it finishes.
So, what's the difference? What are the use cases for each? Are there any conditions when only one of these are created?

Both are created on request and torn down when it finishes.
It is true in the request lifecycle. Flask create the app context, the request context, do some magic, destroy request context, destroy app context.
The application context can exist without a request and that is the reason you have both. For example, if I'm running from a shell, I can create the app_context, without the request and has access to the ´current_app` proxy.
It is a design decision to separate concerns and give you the option to not create the request context. The request context is expensive.
In old Flask's (0.7?), you had only the request context and created the current_app with a Werkzeug proxy. So the application context just create a pattern.
Some docs about appcontext, but you probably already read it: http://flask.pocoo.org/docs/appcontext/

Related

Is there a way to access the "request" and "response" within a server side layout, page or component in nextjs13's app directory?

I'm trying to apply server side authentication in a similar way to how getServerSideProps() works in the older /pages approach to nextjs.
However with this approach getServerSideProps takes a context parameter which contains both the req and res from the server. Which means we could attach headers to the response.
Is this context (or parts) accessible in the new Server Components within nextjs 13?
I know I can access cookies and headers for the request using the next/cookies and next/headers - is there a way to access the response? Current cookies.set() function doesn't work (throws an error), do I need have to wait for this to be implemented?

Flask-SocketIO access session from background task

I have a Flask app for http and web socket (Flask-SocketIO) communication between client and server using gevent. I also use server side session with Flask-Session extension for the app. I run a background task using SocketIO.start_background_task. And from this task, I need to access session information which will be used to emit message using socketio. I get error when accessing session from the task "RuntimeError: Working outside of request context." This typically means that you attempted to use functionality that needed an active HTTP request.
Socket IO instance is created as below-
socket_io = SocketIO(app, async_mode='gevent', manage_session=False)
Is there any issue with this usage. How this issue could be addressed?
Thanks
This is not related to Flask-SocketIO, but to Flask. The background task that you started does not have request and application contexts, so it does not have access to the session (it doesn't even know who the client is).
Flask provides the copy_current_request_context decorator duplicate the request/app contexts from the request handler into a background task.
The example from the Flask documentation uses gevent.spawn() to start the task, but this would be the same for a task started with start_background_task().
import gevent
from flask import copy_current_request_context
#app.route('/')
def index():
#copy_current_request_context
def do_some_work():
# do some work here, it can access flask.request or
# flask.session like you would otherwise in the view function.
...
gevent.spawn(do_some_work)
return 'Regular response'

how to maintain a requestcontext across rest datasource in GraphQL

I wanted to know if i have multiple resolvers and in the resolvers which extends RESTDataSource . if i get access to context using this.context . is that the context that server has created for that particular request ? The reason I am asking this question is :- I see when from a RESTDatSource if i call another RESTDataSource then occasionally the context is getting over ridden for the chained call to a new context ( which is based on another request to server coming in same same time frame)
I don't know which framework you are using but if you are using ApolloServer then you can have different context object for each request. If you can explain where exactly you are facing this issue I can help more... :)

Django test client on an actual server

I'm testing deploying my first Django project using Apache.
I use Django's test client to perform an "internal" GET from my own server, which worked OK locally, but not runnning on the actual server.
The client ends up getting Django error messages, like
Page not found (404) Request Method: GET Request
URL: http://testserver/polls/forms/test1/
How can I get the client's GET to work on the actual server, having the it be performed on the actual http: //my_actual_server_name.something/polls/forms/test1 instead of "testserver" ?
I tried setting SERVER_NAME= ‘my_actual_server_name.something’ in the settings.py file but that's not it.
Django's test client doesn't actually make HTTP requests, it just makes a request object and passes it to your middleware/views.
If your goal is to make an http request to your own server, an easy way is to install requests and do something like
# Some server on the network
requests.get("http://myserver.com/polls/forms/test1/")
# or some server running on the same machine
requests.get("http://12.0.0.1:8000/polls/forms/test1/")
If you just want to use the functionality of some view, you should move that logic into a function and call that from both the view and your other code.
Very tangential side note:
If you're curious about how the test client doesn't make http requests, you can look at the test client's code in the django source (client.get() calls client.generic() which calls client.request() which instantiates WSGIRequest() and then passes that object to your app - which is the request that you receive in your views).

Embedded Jetty 9: How to create a request to pass to RequestDispatcher from scratch?

I am trying to create and feed requests from code (server initialization) to JSPs using Jetty 9.2. This is mainly for warmup/precompilation but also other reasons. I do not have an ongoing request (this is happening at startup) and I would like to avoid the workarounds that actually send requests using the network.
Problem I face right now is that the following line (83) in Jetty's Dispatcher.java fails with a null pointer exception:
Request baseRequest=(request instanceof Request)?((Request)request):HttpChannel.getCurrentHttpChannel().getRequest();
So, how does one properly feed requests code-created requests into embedded Jetty? Pleas help.
Dispatcher requires a valid request.
As the dispatched call requires a bunch of information from the Request (such as method, headers, content encodings, what sort of response encodings are valid, uri, context path, sessions, cookies, security contexts, principals, etc. the list is long)