User-Agent, version and other mysteries

Sean 'Shaleh' Perry shalehperry at attbi.com
Tue Apr 2 11:25:44 EST 2002


On 02-Apr-2002 Stefan Faltz wrote:
> I try to change the "User-Agent" header, i tried it first by something
> like
> 
> urllib.FancyURLopener.version = "Whatever Agent you like"
> 
> that didn't work, what a surprise
> 
> then i ripped some code, and i think it is reasonable
> 
> class AppURLopener(urllib.FancyURLopener):
>     def __init__(self, *args):
>         apply(urllib.FancyURLopener.__init__, (self,) + args)
>         self.version = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
> 5.0; T312461; Q312461)"
> 
> urllib._urlopener = AppURLopener
> 

from the library reference:

For example, applications may want to specify a different `User-Agent' header
than `URLopener' defines.  This can be accomplished with the following code:

     class AppURLopener(urllib.FancyURLopener):
         def __init__(self, *args):
             self.version = "App/1.7"
             urllib.FancyURLopener.__init__(self, *args)

     urllib._urlopener = AppURLopener()

The key here is urllib._urlopener takes a constructed class instance, not the
name of a class.  Also notice how the parent's constructor is called.




More information about the Python-list mailing list