Python Mechanize - how to add a header to a single .open () call?

I am trying to get around a certain service that does not have an API and decided to try Mechanize (usually I use urllib).

How to add a specific header for one open call?

Or is there a way to create an instance of Request with its own headers and then process its instance of mechanize.Browser ?

 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 change the addheaders list, but wow is not ugly!

 browser.addheaders.append(AJAX_HEADER) browser.open('/admin/discounts', urllib.urlencode(pulled_params)) browser.addheaders.pop() 
+4
source share
2 answers

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 a POST request request = urllib2.Request(url, None, header) # here you go response = browser.open(request) print response.geturl() print response.read() response.close() 
+7
source

You can use 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 as follows:

 with ExtraHeaders(browser, [AJAX_HEADER]): browser.open('/admin/discounts', urllib.urlencode(pulled_params)) #requests beyond this point won't have AJAX_HEADER 

Please note that if you are multithreading, any threads accessing the browser and another thread inside the with statement will also have additional headers.

+1
source

All Articles