views:

60

answers:

2

For some reason i am unable to instantiate the set_cookie outside of the MainHandler.. This is a little code to show what im wanting to do.. Can Anyone help??

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from GenCookie import * 

class MainHandler(tornado.web.RequestHandler):
     def get(self):
       g=GenCookie()
       response = g.genCookie()

class GenCookie:
 def genCookie(self):
    print self.request.remote_ip
    print self.cookies
    print self.request.headers
    expires = datetime.datetime.utcnow() + datetime.timedelta(days=365)
    if ("uid" in cookies):
       self.set_cookie("uid", value=cookies["uid"],expires=expires)
    else:
       self.set_cookie("uid", value='dfc278623ab44df2bd501e106e81d146',expires=expires)
    return

Any ideas?

+2  A: 

I thought that explains itself.

set_cookie is a method of tornado.web.RequestHandler

while in your code "self.set_cookie", self refers to object of class GenCookie.

Your code can be modified to pass the necessary reference

class MainHandler(tornado.web.RequestHandler):
     def get(self):
       g=GenCookie(self)
       response = g.genCookie()

class GenCookie:
 def __init__(self, reqHandler):
    self.reqHandler = reqHandler

 def genCookie(self):
    print self.request.remote_ip
    print self.cookies
    print self.request.headers
    expires = datetime.datetime.utcnow() + datetime.timedelta(days=365)
    if ("uid" in cookies):
       self.reqHandler.set_cookie("uid", value=cookies["uid"],expires=expires)
    else:
       self.reqHandler.set_cookie("uid", value='dfc278623ab44df2bd501e106e81d146',expires=expires)
    return
pyfunc
A: 

Thanks for the Response.. Im an amatuer at initialising classes... hehe

Arenstar