I'd like to hear your suggestions on how to effectively store cookies, that are to be used inside a class by other functions. My current code looks like this:
class SomeClass:
def __init__(self, username, password):
self.logged_in = False
self.username = username
self.password = password
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
def _login(self, username, password):
if not self.logged_in:
params = urllib.urlencode({'username': username, 'password': password})
conn = urllib2.Request('http://somedomain.com/login', params)
urllib2.urlopen(conn)
self.logged_in = True
def _checkLogin(self):
if not self.logged_in:
self._login(self.username, self.password)
def doSomeStuffThatRequireCookies(self):
self._checkLogin()
data = urllib2.urlopen(conn).read()
return data
Although above example works, I must build custom Request() if I do NOT want to make request with cookies and I am sure there must be better and more elegant way to do this.
Thank you.