Python Forum
Retrieving Cookies - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Retrieving Cookies (/thread-31079.html)



Retrieving Cookies - whois1230 - Nov-21-2020

Hello, I wrote the following code and ran it:
#!/usr/bin/python3 # Import modules for CGI handling from os import environ import cgi, cgitb if environ.has_key('HTTP_COOKIE'): for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')): (key, value ) = split(cookie, '='); if key == "UserID": user_id = value if key == "Password": password = value print ("User ID = %s" % user_id) print ("Password = %s" % password)
The result I was supposed to get is:
User ID = XYZ Password = XYZ123
I got an error:
Python 3.6.9 (default, Oct 8 2020, 12:12:24) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license()" for more information. >>> ==== RESTART: /home/user/Desktop/Programming/Python/Retrieving Cookies.py ==== Traceback (most recent call last): File "/home/user/Desktop/Programming/Python/Retrieving Cookies.py", line 5, in <module> if environ.has_key('HTTP_COOKIE'): AttributeError: '_Environ' object has no attribute 'has_key' >>> 



RE: Retrieving Cookies - ibreeden - Nov-21-2020

Hi,
Where did you find os.environ has a method has_key()?
I don't see it in the documentation.
I think you should do something like:
var_cookie = environ['HTTP_COOKIE']
... and see what it contains.


RE: Retrieving Cookies - snippsat - Nov-21-2020

has_key is removed in Python 3.
Call it directly environ['HTTP_COOKIE']

This is old code an outdated code,as mention i your other Thread,should not use CGI.


This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.