Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for customising the useragent #16

Merged
merged 2 commits into from Mar 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions client.py
Expand Up @@ -39,7 +39,7 @@ def __hash__(self):
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
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(self, host, path = '/w/', ext = '.php', pool = None, retry_timeout

# Setup connection
if pool is None:
self.connection = http.HTTPPool()
self.connection = http.HTTPPool(clients_useragent)
else:
self.connection = pool

Expand Down
34 changes: 15 additions & 19 deletions http.py
Expand Up @@ -9,18 +9,6 @@

from client import __ver__

class Request(urllib2.Request):
def __init__(self, url, data=None, headers={},
origin_req_host=None, unverifiable=False, head = False):
urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)

self.add_header('User-Agent', 'MwClient-' + __ver__)
self.head = head
def get_method(self):
if self.head: return 'HEAD'
return urllib2.Request.get_method(self)


class CookieJar(dict):
def __init__(self):
dict.__init__(self, ())
Expand Down Expand Up @@ -53,14 +41,20 @@ def __init__(self, name, value):
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):
Expand All @@ -78,7 +72,7 @@ def request(self, method, host, path, headers, data,
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()
Expand Down Expand Up @@ -186,9 +180,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
Expand All @@ -215,7 +211,7 @@ def find_connection(self, host, scheme = 'http'):
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):
Expand Down