クラス内で他の関数が使用するCookieを効果的に保存する方法についての提案をお願いします。私の現在のコードは次のようになります。
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
上記の例は機能しますが、Cookieを使用してリクエストを行いたくない場合は、カスタムRequest()を作成する必要があります。これを行うには、より優れた、よりエレガントな方法が必要です。
ありがとうございました。