I have a code at the top of my program that looks like this:
cj=cookielib.CookieJar()
#Process Hadlers
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#install opener, now all the calls to urllib2.urlopen use this opener
urllib2.install_opener(opener)
opener.addheaders=[
('User-Agent', 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9850; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.115 Mobile Safari/534.11+'),
# ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Accept-Language', 'en-gb,en;q=0.5'),
('Accept-Encoding', 'gzip,deflate'),
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
('Keep-Alive', '115'),
('Connection', 'keep-alive'),
('Cache-Control', 'max-age=0'),
('Referer', 'http://yahoo.com'),
]
So now I want to move this code to a different class, because I don’t want to copy and paste the code across my program.
As far as I understand urllib2.install_opener(opener) add all the headers, cookies and so on to the urllib2 when I call urlopen().
Want to move it to some class but don’t really know what this code supposed to return. Plus if I move it to the __init__ method, it won’t work, because init does not return anything.
You can just have this code in a module named
urlhandler.pyand use theopenerthat you have defined above.from urlhandler import openerthat should work. The code initializes the opener with all the appropriate handlers for it urllib2 to accept requests.With this, you will be using
openerto open the request object and get the response back