Shinyshell Community Forums > Coding >
Cookies in Python?


[1]


January 29 02009, 07:40 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
The other day I decided that I would completely code PokéNova in Python, which would have been a great decision, if I actually half-knew how to code it. I realised that the thing I was having the most trouble with was cookies - how to set, destroy and check (the value of) them.

So, can someone please tell me how to do it? And if they are really, really nice, could they please include an example script in their post?

Also, I'd like to know how import works, and how I can make my own modules and set it up so that I can import them in my scripts.

Thanks!
______________________________
Lazurane

January 29 02009, 21:33 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173

import os 
import Cookie

# load the cookie values provided
ck = Cookie.Cookie(os.environ.get("HTTP_COOKIE", ""))

# example getting a cookie value:
# for example, a userid cookie might be set..
userid = ck["userid"].value
import os
import Cookie

# load the cookie values provided
ck = Cookie.Cookie(os.environ.get("HTTP_COOKIE", ""))

# example getting a cookie value:
# for example, a userid cookie might be set..
userid = ck["userid"].value

]]>


There is probably a way to set cookies too using the Cookie module, but I prefer using the HTTP way directly, since my templating system doesn't print stuff right away, rather after I have generated all the content (a requirement of FCGI)

print "Set-Cookie: userid=%s; expires=%s" % (cookie_userid, cookie_expiry) 
print "Set-Cookie: userid=%s; expires=%s" % (cookie_userid, cookie_expiry)
]]>

February 09 02009, 09:18 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
And to destroy/expire cookies (on logout for example, would the code be like this?:

cookie_userid = false 
cookie_expiry = "-3600"
print "Set-Cookie: userid=%s; expires=%s" % (cookie_userid, cookie_expiry)
cookie_userid = false
cookie_expiry = "-3600"
print "Set-Cookie: userid=%s; expires=%s" % (cookie_userid, cookie_expiry)
]]>


______________________________
Lazurane

February 09 02009, 13:15 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173

Since cookies use timestamps when you're setting them, not relative counts of seconds, an expiry time of 0 will probably work better.


[1]



Forum Information
  Currently Active Members [detailed] (0 members and ? guests)
-
Forum Statistics
Topics: 0, Posts: 0, Members: 108.
Welcome to our newest member, adamthephantump
Most members online was 5, on August 28 2009, at 21:49:28.
Legend
    Forums with unread topics in them are indicated by a strong yellow colouring around the forum icon.
    Forums with no unread topics have the standard pale yellow colouring around the forum icon.
    Forums with a blue arrow icon will redirect you to a non-forum page.
    Locked forums have a little padlock by their icon. You won't be able to post in these forums.
Shinyshell Home | Contact | Staff List | Archive | Top 

Conventional Login

Don't have an account? You may want to create one.

OpenID Login
OpenID login and registration is usable, but not finished.
What is OpenID?
Search

(advanced search)
Site Stats
  Total members: 108
  Latest member: adamthephantump
  Members currently online: 0
  Most online: 5 - Aug 28, 2009 (21:49)
  Front page hits: 88118
Developer info
  Site version: 3.5 Alpha
  16 queries - 9 templates
Under the Spotlight
Collide Site
Collide make fabulously dreamy electronic-industrial music, they're one of my favourite bands! Give them a chance to take control of your life - myspace | youtube - "Euphoria".

Collide Site - Hits: 4926

5/5 (2) | Rate this site?