From 0c3543c336705c575e8e345b9ae2218af7cf90bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= <danmichaelo@gmail.com> Date: Sat, 23 Feb 2013 22:24:10 +0000 Subject: [PATCH] add configurable user-agent to comply with http://meta.wikimedia.org/wiki/User-Agent_policy --- client.py | 3 ++- http.py | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/client.py b/client.py index 1a3f60e..88b1985 100644 --- a/client.py +++ b/client.py @@ -40,7 +40,8 @@ class Site(object): api_limit = 500 def __init__(self, host, path = '/w/', ext = '.php', pool = None, retry_timeout = 30, max_retries = 25, wait_callback = lambda *x: None, - max_lag = 3, compress = True, force_login = True, do_init = True): + max_lag = 3, compress = True, force_login = True, do_init = True, + user_agent = 'MwClient-' + __ver__): # Setup member variables self.host = host self.path = path diff --git a/http.py b/http.py index e33b4c8..fa7ba22 100644 --- a/http.py +++ b/http.py @@ -54,9 +54,10 @@ class HTTPPersistentConnection(object): http_class = httplib.HTTPConnection scheme_name = 'http' - def __init__(self, host, pool = None): + def __init__(self, host, pool = None, user_agent='MwClient-' + __ver__): self.cookies = {} self.pool = pool + self.user_agent = user_agent if pool: self.cookies = pool.cookies self._conn = self.http_class(host) self._conn.connect() @@ -78,7 +79,7 @@ class HTTPPersistentConnection(object): headers = {} headers['Connection'] = 'Keep-Alive' - headers['User-Agent'] = 'MwClient/' + __ver__ + headers['User-Agent'] = self.user_agent headers['Host'] = host if host in self.cookies: headers['Cookie'] = self.cookies[host].get_cookie_header() @@ -186,9 +187,10 @@ class HTTPSPersistentConnection(HTTPPersistentConnection): class HTTPPool(list): - def __init__(self): + def __init__(self, user_agent='MwClient-' + __ver__): list.__init__(self) self.cookies = {} + self.user_agent = user_agent def find_connection(self, host, scheme = 'http'): if type(host) is tuple: scheme, host = host @@ -215,7 +217,7 @@ class HTTPPool(list): cls = HTTPSPersistentConnection else: raise RuntimeError('Unsupported scheme', scheme) - conn = cls(host, self) + conn = cls(host, self, user_agent=self.user_agent) self.append(([(scheme, host)], conn)) return conn def get(self, host, path, headers = None): -- GitLab