Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieving Cookies
#1
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'
>>> 
Reply
#2
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.
Reply
#3
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.
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to accept facebook cookies using python selenium? pablo86ad 0 189 Apr-06-2024, 09:19 PM
Last Post: pablo86ad
  KeyError while retrieving ESPN data john317ab 2 812 Nov-29-2023, 09:07 PM
Last Post: john317ab
  .get() not retrieving value? Sedos101 2 565 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 2,320 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 2,338 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Problem: Retrieving Form data PythonDev 3 3,098 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  Cookies Donald 3 2,827 Apr-15-2020, 07:06 AM
Last Post: DPaul
  Retrieving dictionary keys within with another dictionay bazcurtis 8 2,828 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  Retrieving items from JSON bazcurtis 12 5,068 Oct-27-2019, 05:18 PM
Last Post: bazcurtis
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,053 Sep-17-2019, 05:21 PM
Last Post: swechsler

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020