Created
July 1, 2010 16:30
-
-
Save sroccaserra/7670ff1915d6e8ff839e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
class MyHttpBrowser(object): | |
def __init__(self, baseUrl, loginUrl, username, password, digest = True): | |
self.baseUrl = baseUrl | |
self.loginUrl = loginUrl | |
self.username = username | |
self.password = password | |
auth_handler = urllib2.HTTPDigestAuthHandler() if digest else urllib2.HTTPBasicAuthHandler() | |
auth_handler.add_password(realm = 'Some Realm', | |
uri = baseUrl, | |
user = 'toto', | |
passwd = '12345') | |
proxy_handler = urllib2.ProxyHandler({}) | |
http_error_handler = MyHttpErrorHandler() | |
self.opener = urllib2.build_opener(auth_handler, urllib2.HTTPCookieProcessor(), | |
proxy_handler, http_error_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
class MyHttpErrorHandler(urllib2.HTTPDefaultErrorHandler): | |
def http_error_default(self, req, fp, code, msg, hdrs): | |
superMethod = urllib2.HTTPDefaultErrorHandler.http_error_default | |
try: | |
jsonResponse = json.load(fp) | |
message = jsonResponse['errorMessage'] | |
if message: | |
raise urllib2.HTTPError(req.get_full_url(), code, message, hdrs, fp) | |
else: | |
superMethod(self, req, fp, code, msg, hdrs) | |
except json.JSONDecodeError: | |
superMethod(self, req, fp, code, msg, hdrs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment