# HG changeset patch # Parent eb1d271a0b20fa97a1bcc1a8cbba411a76fe792a Issue #25570: Add example of customizing User-Agent via add_header() diff -r eb1d271a0b20 -r cda51eb0bfdf Doc/library/urllib.request.rst --- a/Doc/library/urllib.request.rst Sun Nov 08 23:20:40 2015 +0000 +++ b/Doc/library/urllib.request.rst Mon Nov 09 00:58:30 2015 +0000 @@ -181,7 +181,7 @@ *headers* should be a dictionary, and will be treated as if :meth:`add_header` was called with each key and value as arguments. - This is often used to "spoof" the ``User-Agent`` header, which is + This is often used to "spoof" the ``User-Agent`` header value, which is used by a browser to identify itself -- some HTTP servers only allow requests coming from common browsers as opposed to scripts. For example, Mozilla Firefox may identify itself as ``"Mozilla/5.0 @@ -1098,6 +1098,9 @@ Examples -------- +In addition to the examples below, more examples are given in +:ref:`urllib-howto`. + This example gets the python.org main page and displays the first 300 bytes of it. :: @@ -1207,6 +1210,8 @@ import urllib.request req = urllib.request.Request('http://www.example.com/') req.add_header('Referer', 'http://www.python.org/') + # Customize the default User-Agent header value: + req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)') r = urllib.request.urlopen(req) :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to