1
\$\begingroup\$

I am trying to determine what the most appropriate strategy for session management and authentication is for a middleware application which acts to receive a request, send it over to a database, and essentially echo those results back to the client.

I have a variety of functions that look like this

@app.route('/rest/results_mod', methods=['POST'])
@crossdomain(origin='*')
def rest_results_mod():
    if request.method == 'POST':
        with requests.session() as request_session:
                request_session.trust_env = False
                request_session.auth = HTTPDigestAuth("admin", "admin")
                posted_form = request.form
                text_search = posted_form['searchTerm']

                params = urllib.parse.urlencode({"rs:searchTerm": "{0}".format(text_search)})

                response = request_session.get(SHOW_SEARCH_MOD+'?{0}'.format(params))
                #print(response.content)

        return json.dumps(json.loads(response.content)), 200, {'Content-Type': 'application/json'}

For each request, I am currently creating a session, and issuing the request as context managed to make sure it closes. I have about 5 or so of these functions each with its own session invocation.

My question is:

Is there any reason this is a bad strategy? Should I initiate a session only when the application gets initalized, and then have each request use the open session? I've tried both ways, they both seem to work in limited testing, but my question is essentially are there any pro's and con's to one way or the other? Or is one way "preferred" or standard for this type of task?

Thank you for reading, I look forward to your advice

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.