From 773adf9b8ff771d0794b640ecf3a23312d68539a Mon Sep 17 00:00:00 2001 From: eug48 <eug48@server.fake> Date: Thu, 21 Mar 2013 15:30:51 +1000 Subject: [PATCH] Support for customising the useragent. This is needed to nicely use the MediaWiki API. --- client.py | 4 ++-- http.py | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/client.py b/client.py index ccf1793..0b96315 100644 --- a/client.py +++ b/client.py @@ -39,7 +39,7 @@ class WaitToken(object): 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_retries = 25, wait_callback = lambda *x: None, clients_useragent = None, max_lag = 3, compress = True, force_login = True, do_init = True): # Setup member variables self.host = host @@ -70,7 +70,7 @@ class Site(object): # Setup connection if pool is None: - self.connection = http.HTTPPool() + self.connection = http.HTTPPool(clients_useragent) else: self.connection = pool diff --git a/http.py b/http.py index e33b4c8..2aa0a6d 100644 --- a/http.py +++ b/http.py @@ -53,14 +53,20 @@ class Cookie(object): class HTTPPersistentConnection(object): http_class = httplib.HTTPConnection scheme_name = 'http' + useragent = None - def __init__(self, host, pool = None): - self.cookies = {} - self.pool = pool - if pool: self.cookies = pool.cookies + def __init__(self, host, pool = None, clients_useragent = None): self._conn = self.http_class(host) self._conn.connect() self.last_request = time.time() + self.cookies = {} + + self.pool = pool + if pool: self.cookies = pool.cookies + + clients_useragent = clients_useragent or "" + if clients_useragent != "": clients_useragent += " " + self.useragent = clients_useragent + 'MwClient/' + __ver__ def request(self, method, host, path, headers, data, raise_on_not_ok = True, auto_redirect = True): @@ -78,7 +84,7 @@ class HTTPPersistentConnection(object): headers = {} headers['Connection'] = 'Keep-Alive' - headers['User-Agent'] = 'MwClient/' + __ver__ + headers['User-Agent'] = self.useragent headers['Host'] = host if host in self.cookies: headers['Cookie'] = self.cookies[host].get_cookie_header() @@ -186,9 +192,11 @@ class HTTPSPersistentConnection(HTTPPersistentConnection): class HTTPPool(list): - def __init__(self): + def __init__(self, clients_useragent = None): list.__init__(self) self.cookies = {} + self.clients_useragent = clients_useragent + def find_connection(self, host, scheme = 'http'): if type(host) is tuple: scheme, host = host @@ -215,7 +223,7 @@ class HTTPPool(list): cls = HTTPSPersistentConnection else: raise RuntimeError('Unsupported scheme', scheme) - conn = cls(host, self) + conn = cls(host, self, self.clients_useragent) self.append(([(scheme, host)], conn)) return conn def get(self, host, path, headers = None): -- GitLab