urllib - changing the user agent

Samuel Walters swalters_usenet at yahoo.com
Fri Jan 9 16:17:24 EST 2004


|Thus Spake John J. Lee On the now historical date of Fri, 09 Jan 2004
20:16:54 +0000|
 
> or again, you can set .addheaders on OpenerDirector (which will cause
> those headers to be added to all requests).

This, however, does not stop the original User-agent header to be sent,
and google still filters out the request.  Instead, it just causes a
second user-agent to be sent.

Here is the very bad code I used to solve this problem.  There are better
ways, I assure you, but it should point you in the right direction.  You
should probably do this with inheritance, but for my quick script, this is
what I did.

----
#Override the default OpenerDirector Class Init.
#OpenerDirector *insists* on adding a User-Agent 
#That some websites don't like.
foo = OpenerDirector.__init__
def bar(self, Agent='None'):
         foo(self)
         self.addheaders = [('User-Agent',Agent)]
 
OpenerDirector.__init__ = bar
-----

HTH

Sam Walters.



-- 
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
    Do you really want to go there?"""




More information about the Python-list mailing list