Shinyshell Community Forums > Coding >
Python Cookies :3


[1]


October 31 02009, 18:05 GMT
Peter*
A Pythonic One

Peter's avatar
Location: US
Post count: 99

I was digging through the old threads and trying to figure out how to make a cookie in Python. Here's what I have so far. This is the whole file and I'm not trying to give you a lot to sift through xD I just left all of it in case something was interfering.

#!/usr/bin/env python 

print "Content-type: text/html\n\n"

import cgi, cgitb, sys, time
cgitb.enable()
sys.path.append("/home/toxic_elegant/python")
from sqlalchemy import create_engine, MetaData, Table, Column, desc
from sqlalchemy.orm import mapper, sessionmaker
from sqlalchemy.types import Integer, Unicode, Date, Text
from peter import engine, users, User, poems, Poem

cookie_expiry = time.time() + 315360000
print "Set-Cookie: betacookie=%s; expires=%s" % (4, cookie_expiry)

f = "head.html"
print open(f).read()

form = cgi.FieldStorage()

Session = sessionmaker(bind=engine)
session = Session()

mapper(Poem, poems)
mapper(User, users)


if "id" in form:

id = form.getfirst("id")

if id == "main":
print "MAIN."

elif id == "poetry":
print "<ul>"

for row in session.query(Poem).order_by(desc(Poem.id)):
user = session.query(User).filter(User.id==row.userid)
print """<li><a href="http://elegance.chanlu.org/beta/poem/%d">%s</a> (by %s)</li>""" % (row.id, row.title, user[0].user)

print "</ul>"

f = "foot.html"
print open(f).read()
#!/usr/bin/env python

print "Content-type: text/html\n\n"

import cgi, cgitb, sys, time
cgitb.enable()
sys.path.append("/home/toxic_elegant/python")
from sqlalchemy import create_engine, MetaData, Table, Column, desc
from sqlalchemy.orm import mapper, sessionmaker
from sqlalchemy.types import Integer, Unicode, Date, Text
from peter import engine, users, User, poems, Poem

cookie_expiry = time.time() + 315360000
print "Set-Cookie: betacookie=%s; expires=%s" % (4, cookie_expiry)

f = "head.html"
print open(f).read()

form = cgi.FieldStorage()

Session = sessionmaker(bind=engine)
session = Session()

mapper(Poem, poems)
mapper(User, users)


if "id" in form:

id = form.getfirst("id")

if id == "main":
print "MAIN."

elif id == "poetry":
print "<ul>"

for row in session.query(Poem).order_by(desc(Poem.id)):
user = session.query(User).filter(User.id==row.userid)
print """<li><a href="http://elegance.chanlu.org/beta/poem/%d">%s</a> (by %s)</li>""" % (row.id, row.title, user[0].user)

print "</ul>"

f = "foot.html"
print open(f).read()
]]>

October 31 02009, 18:51 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173
hr
You can't end the header output with \n\n, and then send additional headers. What you need to do is print headers with single line end characters until the last one, which has two.

Since Python's print command automatically puts a newline character after any output unless you put a comma behind it, you only need to manually print one:

print "Header-name: value" 
print "Header-name: value"
print "Last-header: value\n"

print content
print "Header-name: value"
print "Header-name: value"
print "Last-header: value\n"

print content
]]>


So in your case, you need to print the cookie header before:

cookie_expiry = time.time() + 315360000  
print "Set-Cookie: betacookie=%s; expires=%s" % (4, cookie_expiry)

print "Content-type: text/html\n"
cookie_expiry = time.time() + 315360000
print "Set-Cookie: betacookie=%s; expires=%s" % (4, cookie_expiry)

print "Content-type: text/html\n"
]]>

November 03 02009, 12:55 GMT
Peter*
A Pythonic One

Peter's avatar
Location: US
Post count: 99

Super-excellent! Thank you. ^_^


[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: 87993
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: 4595

5/5 (2) | Rate this site?