I am working with Python’s SimpleCookie and I ran into this problem and I am not sure if it is something with my syntax or what. Also, this is classwork for my Python class so it is meant to teach about Python so this is far from the way I would do this in the real world.
Anyway, so basically I am keeping information input into a form in a cookie. I am attempting to append to the previous cookie with the new information entered. But for some reason on the third entry of data the cookie suddenly gets ‘\’ in it. I am not sure where they are coming from though.
This is the type of output I am getting:
‘\’\\’\\\\’test:more\\\\’:rttre\\’:more\’:and more’
#!/usr/local/bin/python import cgi,os,time,Cookie #error checking import cgitb cgitb.enable() if 'HTTP_COOKIE' in os.environ: cookies = os.environ['HTTP_COOKIE'] cookies = cookies.split('; ') for myCookie in cookies: myCookie = myCookie.split('=') name = myCookie[0] value = myCookie[1] if name == 'critter' : hideMe = value #import critterClass #get info from form form = cgi.FieldStorage() critterName = form.getvalue('input') input2 = form.getvalue('input2') hiddenCookie = form.getvalue('hiddenCookie') hiddenVar = form.getvalue('hiddenVar') #make cookie cookie = Cookie.SimpleCookie() #set critter Cookie if critterName is not None: cookie['critter'] = critterName #If already named else: #if action asked, append cookie if input2 is not None: cookie['critter'] = hideMe+':'+input2 else: cookie['critter'] = 'default' print cookie print 'Content-type: text/html\n\n' if ((critterName is None) and (input2 is None)): print ''' <form name='critter' id='critter' method='post' action='critter.py'> <label for='name'>Name your pet: <input type='text' name='input' id='input' /></label> <input type='submit' name='submit' id='submit' value='Submit' /> </form> ''' else: formTwo =''' <form name='critter2' id='critter2' method='post' action='critter.py'> <label for='name'>%s wants to: <input type='text' name='input2' id='input2' /></label> <input type='hidden' name='hiddenVar' id='hiddenVar' value='%s' /> <input type='submit' name='submit' id='submit' value='Submit' /> </form> [name,play,feed,mood,levels,end] ''' print formTwo % (critterName,critterName) if 'HTTP_COOKIE' in os.environ: cookies = os.environ['HTTP_COOKIE'] cookies = cookies.split('; ') for myCookie in cookies: myCookie = myCookie.split('=') name = myCookie[0] value = myCookie[1] if name == 'critter' : print 'name'+name print 'value'+value
As explained by others, the backslashes are escaping double quote characters you insert into the cookie value. The (hidden) mechanism in action here is the
SimpleCookieclass. TheBaseCookie.output()method returns a string representation suitable to be sent as HTTP headers. It will insert escape characters (backslashes) before double quote characters and before backslash characters.The
statement activates
BaseCookie.output().On each trip your string makes through the cookie’s
output()method, backslashes are multiplied (starting with the 1st pair of quotes).