Python, Urllib2 and Openers

390 views Asked by At

I have a situation where depending on what the user selects in my package, I need to install multiple handlers.

Example I have 3 handlers. If a user says they want to use a proxy, then you need to add a proxy handler, but this isn't always the situation.

So can you call opener = urllib2.build_opener() multiple times without overwriting the existing handlers?

example: opener = urllib2.build_opener(RedirectHandler()) opener = urllib2.build_opener(ProxyHandler())... etc...

I don't need all the handlers all the time, and I don't feel like making use cases for each situation.

Thank you

1

There are 1 answers

0
code base 5000 On

The answer turned out to be quite simple.

handlers = [HandlerObj1(),..., HandlerObjn()]
urllib2.build_opener(*handlers)

That's it! It was just a *.