self.debug = False
self.trycount = trycount
- def build_opener(self, url, postdata = None, extraheaders = None, forbid_redirect = False):
- if extraheaders is None:
- extraheaders = {}
-
- txheaders = {
- 'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png',
- 'Accept-Language': 'en,en-us;q=0.5',
- 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
- }
- for key, value in extraheaders.iteritems():
- txheaders[key] = value
- req = urllib2.Request(url, postdata, txheaders)
- self.cookies.add_cookie_header(req)
- if forbid_redirect:
- redirector = HTTPNoRedirector()
- else:
- redirector = urllib2.HTTPRedirectHandler()
-
- http_handler = urllib2.HTTPHandler(debuglevel=self.debug)
- https_handler = urllib2.HTTPSHandler(debuglevel=self.debug)
-
- u = urllib2.build_opener(
- http_handler,
- https_handler,
- urllib2.HTTPCookieProcessor(self.cookies),
- redirector
- )
- u.addheaders = [(
- 'User-Agent',
- 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4'
- )]
- if not postdata is None:
- req.add_data(postdata)
- return (req, u)
-
def download(self, url,
- postdata = None, extraheaders = None, forbid_redirect = False,
+ postdata = None, extraheaders = None, forbidRedirect = False,
trycount = None, only_head = False,
):
"""Download an URL with GET or POST methods.
@param postdata: It can be a string that will be POST-ed to the URL.
When None is given, the method will be GET instead.
@param extraheaders: You can add/modify HTTP headers with a dict here.
- @param forbid_redirect: Set this flag if you do not want to handle
+ @param forbidRedirect: Set this flag if you do not want to handle
HTTP 301 and 302 redirects.
@param trycount: Specify the maximum number of retries here.
0 means no retry on error. Using -1 means infinite retring.
while True:
try:
- req, u = self.build_opener(url, postdata, extraheaders, forbid_redirect)
+ req, u = self._build_opener(url, postdata, extraheaders, forbidRedirect)
openerdirector = u.open(req)
if self.debug:
_moduleLogger.info("%r - %r" % (req.get_method(), url))
# Retry :-)
_moduleLogger.info("MozillaEmulator: urllib2.URLError, retryting %d" % cnt)
+ def _build_opener(self, url, postdata = None, extraheaders = None, forbidRedirect = False):
+ if extraheaders is None:
+ extraheaders = {}
+
+ txheaders = {
+ 'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png',
+ 'Accept-Language': 'en,en-us;q=0.5',
+ 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
+ }
+ for key, value in extraheaders.iteritems():
+ txheaders[key] = value
+ req = urllib2.Request(url, postdata, txheaders)
+ self.cookies.add_cookie_header(req)
+ if forbidRedirect:
+ redirector = HTTPNoRedirector()
+ _moduleLogger.info("Redirection disabled")
+ else:
+ redirector = urllib2.HTTPRedirectHandler()
+ _moduleLogger.info("Redirection enabled")
+
+ http_handler = urllib2.HTTPHandler(debuglevel=self.debug)
+ https_handler = urllib2.HTTPSHandler(debuglevel=self.debug)
+
+ u = urllib2.build_opener(
+ http_handler,
+ https_handler,
+ urllib2.HTTPCookieProcessor(self.cookies),
+ redirector
+ )
+ u.addheaders = [(
+ 'User-Agent',
+ 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4'
+ )]
+ if not postdata is None:
+ req.add_data(postdata)
+ return (req, u)
+
def _read(self, openerdirector, trycount):
chunks = []
elif 'uri' in headers:
newurl = headers.getheaders('uri')[0]
e.newurl = newurl
+ _moduleLogger.info("New url: %s" % e.newurl)
raise e
@note Thread agnostic
"""
username, password = self._credentials
- loggedIn = self._phoneBackends[self._defaultBackendId].login(username, password)
+ try:
+ loggedIn = self._phoneBackends[self._defaultBackendId].login(username, password)
+ except Exception:
+ # Retry in case the redirect failed
+ # luckily is_authed does everything we need for a retry
+ loggedIn = self._phoneBackends[self._defaultBackendId].is_authed(True)
+ if not loggedIn:
+ raise
+ _moduleLogger.info("Redirection failed on initial login attempt, auto-corrected for this")
if loggedIn:
self._credentials = username, password
_moduleLogger.info("Logged into %r through settings" % self._phoneBackends[self._defaultBackendId])
banner = hildonize.show_busy_banner_start(self._window, "Logging In...")
try:
username, password = credentials
- loggedIn = self._phoneBackends[tmpServiceId].login(username, password)
+ try:
+ loggedIn = self._phoneBackends[tmpServiceId].login(username, password)
+ except Exception:
+ # Retry in case the redirect failed
+ # luckily is_authed does everything we need for a retry
+ loggedIn = self._phoneBackends[self._defaultBackendId].is_authed(True)
+ if not loggedIn:
+ raise
+ _moduleLogger.info("Redirection failed on initial login attempt, auto-corrected for this")
finally:
with gtk_toolbox.gtk_lock():
hildonize.show_busy_banner_end(banner)