From: Stefan Schantl Date: Mon, 9 Jun 2014 19:34:03 +0000 (+0200) Subject: Fix generation of the basic auth http header. X-Git-Tag: 001~75 X-Git-Url: http://git.ipfire.org/?p=ddns.git;a=commitdiff_plain;h=d4c5c0d5d1a9c06430276dff0afbce02fa1bc3cd Fix generation of the basic auth http header. --- diff --git a/src/ddns/system.py b/src/ddns/system.py index 6057ef4..8fa5c42 100644 --- a/src/ddns/system.py +++ b/src/ddns/system.py @@ -19,6 +19,7 @@ # # ############################################################################### +import base64 import re import urllib import urllib2 @@ -86,7 +87,7 @@ class DDNSSystem(object): return match.group(1) - def send_request(self, url, method="GET", data=None, timeout=30): + def send_request(self, url, method="GET", data=None, username=None, password=None, timeout=30): assert method in ("GET", "POST") # Add all arguments in the data dict to the URL and escape them properly. @@ -102,6 +103,11 @@ class DDNSSystem(object): req = urllib2.Request(url, data=data) + if username and password: + basic_auth_header = self._make_basic_auth_header(username, password) + print repr(basic_auth_header) + req.add_header("Authorization", "Basic %s" % basic_auth_header) + # Set the user agent. req.add_header("User-Agent", self.USER_AGENT) @@ -144,6 +150,17 @@ class DDNSSystem(object): return "&".join(args) + def _make_basic_auth_header(self, username, password): + authstring = "%s:%s" % (username, password) + + # Encode authorization data in base64. + authstring = base64.encodestring(authstring) + + # Remove any newline characters. + authstring = authstring.replace("\n", "") + + return authstring + def get_address(self, proto): assert proto in ("ipv6", "ipv4")