diff --git a/client.py b/client.py index 1a3f60ef88b31e95225bd58fc968915cd00e3f6e..88b1985c275cdb47a6a0a5e04dbde4f0698f270d 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 e33b4c882673f89099f41b89be625c29eeb4af76..fa7ba222be65464b39bf14520fa71fa85f311251 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):