Set a cookie and extract it using Python and WSGI

there are many questions similar to this, but none of them helped me. I mainly use the WSGI start_response () method. I tried to set the dummy header in the response with the tuple [("Set-Cookie", "token = THE_TOKEN")] and add it to start the response as follows:

status = '200 OK' response = 'Success' start_response(status,[('Set-Cookie', "DMR_TOKEN=DMR_TOKEN")]) return response 

I'm not sure if it works correctly, but here is the setting for cookies . Now let's assume that the header is correct, and in the following queries I want to authenticate the token. What would be the correct way to catch this cookie / header set in the past?

I read and found that I need something like this:

 (environ.get("HTTP_COOKIE","")) 

but this all the time gave an empty string, so I just assume that the header / cookie is set incorrectly.

Thanks guys,

+4
source share
1 answer

I think you need to explicitly specify the path to get useful behavior from cookies, try something like: ...

 from Cookie import SimpleCookie def my_app(environ, start_response): session_cookie = SimpleCookie() session_cookie['session'] = "somedata" session_cookie['session']["Path"] = '/' headers = [] headers.extend(("set-cookie", morsel.OutputString()) for morsel in session_cookie.values()) start_response("200 OK", headers) 
+6
source

All Articles