Reputation: 118488
I'm trying to get around a certain service not having an API and decided to try Mechanize (I normally use urllib).
How do I add a specific header for one open
call?
Or is there a way to construct a Request instance with its own headers, then have my mechanize.Browser
instance handle it?
browser = mechanize.Browser()
headers = [
('Accept', 'text/javascript, text/html, application/xml, text/xml, */*'),
('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'),
('User-Agent', 'Foobar'),
]
browser.addheaders = headers
# log in, do stuff, etc.
# here, for this one browser request, I need to add an AJAX header
browser.open('/a_url_to_ajax_post/', urllib.urlencode({'foo': 'bar'}))
My workaround is to temporarily modify the addheaders list, but wow that is ugly!
browser.addheaders.append(AJAX_HEADER)
browser.open('/admin/discounts', urllib.urlencode(pulled_params))
browser.addheaders.pop()
Upvotes: 4
Views: 10062
Reputation: 5967
You could make use of the python with
statement. Make a class like this:
class ExtraHeaders(object):
def __init__(self, br, headers):
self.extra_headers = headers
self.br = br
def __enter__(self):
self.old_headers = self.br.addheaders
self.br.addheaders = self.extra_headers + [h for h in self.br.addheaders if
not reduce(
lambda accum, ex_h: accum or ex_h[0] == h[0],self.extra_headers,False)]
return self.br
def __exit__(self, type, value, traceback):
self.br.addheaders = self.old_headers
Then use it this way:
with ExtraHeaders(browser, [AJAX_HEADER]):
browser.open('/admin/discounts', urllib.urlencode(pulled_params))
#requests beyond this point won't have AJAX_HEADER
Note that if you're multithreading, any threads accessing the browser while another thread is inside the with statement will have the extra headers too.
Upvotes: 2
Reputation: 4983
Do it like this:
import mechanize
import urllib2
browser = mechanize.Browser()
# setup your header, add anything you want
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer': 'http://whateveritis.com'}
url = "http://google.com"
# wrap the request. You can replace None with the needed data if it's a POST request
request = urllib2.Request(url, None, header)
# here you go
response = browser.open(request)
print response.geturl()
print response.read()
response.close()
Upvotes: 7