return self._guess_external_ip_address(url, **kwargs)
- def send_request(self, url, method="GET", data=None, username=None, password=None,
- timeout=30, expect=bytes):
+ def send_request(self, url, method="GET", data=None, authorization=None,
+ username=None, password=None, timeout=30, expect=bytes):
# Add all arguments in the data dict to the URL and escape them properly
if data:
if method == "GET":
req = urllib.request.Request(url, method=method, data=data)
+ # Authenticate using HTTP Basic Auth
if username and password:
- basic_auth_header = self._make_basic_auth_header(username, password)
- req.add_header("Authorization", "Basic %s" % basic_auth_header.decode())
+ # Fail if too much authentication data has been passed
+ if authorization:
+ raise ValueError("You cannot pass an Authorization header as well as username and password")
+
+ # Compose the Authorization: header
+ authorizaton = self._make_basic_auth_header(username, password)
+
+ # Set the Authorization: header
+ if authorization:
+ req.add_header("Authorization", authorization)
# Set the user agent.
req.add_header("User-Agent", self.USER_AGENT)
# Encode authorization data in base64.
authstring = base64.b64encode(authstring.encode())
- return authstring
+ return "Basic %s" % authstring.decode()
def get_address(self, proto):
"""