mysteries of urllib/urllib2

O.R.Senthil Kumaran orsenthil at users.sourceforge.net
Tue Jul 3 15:26:18 EDT 2007


* Adrian Smith <adrian_p_smith at yahoo.com> [2007-07-03 08:14:32]:

> some access. Apparently there's a way to change the user-agent string
> by subclassing urllib's URLopener class, but that's beyond my comfort
> zone at present.

Read the urllib2 how-to located at ActiveState Documentation pages.
That gives the concise snippets as how you will set the USER-AGENT string.
<snip>
import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }
headers = { 'User-Agent' : user_agent }

data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
</snip>

-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org



More information about the Python-list mailing list