Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added runtime cookie #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions pyzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,37 @@ class CookieAuthXMLRPCTransport(xmlrpclib.SafeTransport):
"""

def __init__(self, cookiefile = None, user_agent = None):
self.cookiefile = cookiefile or "cookies.txt"
self.cookiefile = cookiefile
self.runtime_cookie = cookielib.LWPCookieJar()
self.user_agent = user_agent or create_user_agent()
xmlrpclib.SafeTransport.__init__(self)

def send_cookie_auth(self, connection):
"""Include Cookie Authentication data in a header"""
logging.debug("Sending cookie")
cj = cookielib.LWPCookieJar()
cj.load(self.cookiefile)
for cookie in cj:
connection.putheader("Cookie", "%s=%s" % (cookie.name,cookie.value))
if self.cookiefile:
cj = cookielib.LWPCookieJar()
cj.load(self.cookiefile)
for cookie in cj:
connection.putheader("Cookie", "%s=%s" % (cookie.name,cookie.value))
else:
# cookie file not set use runtime
for cookie in self.runtime_cookie:
connection.putheader("Cookie", "%s=%s" % (cookie.name,cookie.value))


## override the send_host hook to also send authentication info
def send_host(self, connection, host):
xmlrpclib.Transport.send_host(self, connection, host)
if os.path.exists(self.cookiefile):
logging.debug(" Sending back cookie header")
if self.cookiefile:
if os.path.exists(self.cookiefile):
logging.debug("Sending back cookie header")
self.send_cookie_auth(connection)
else:
logging.debug("Sending back runtime cookie header")
self.send_cookie_auth(connection)


def request(self, host, handler, request_body, verbose=0):
# dummy request class for extracting cookies
class CookieRequest(urllib2.Request):
Expand All @@ -79,7 +91,8 @@ def info(self):
errcode, errmsg, headers = h.getreply()
cresponse = CookieResponse(headers)
cj.extract_cookies(cresponse, crequest)
if len(cj) >0 and not os.path.exists(self.cookiefile):
self.runtime_cookie.extract_cookies(cresponse, crequest)
if len(cj) >0 and self.cookiefile: # and not os.path.exists(self.cookiefile):
logging.debug("Saving cookies in cookie jar")
cj.save(self.cookiefile)
if errcode != 200:
Expand Down