[Python-checkins] cpython (2.7): Issue #25570: Add example of customizing User-Agent via add_header()

martin.panter python-checkins at python.org
Wed Jun 1 04:29:43 EDT 2016


https://hg.python.org/cpython/rev/3aa49900869b
changeset:   101583:3aa49900869b
branch:      2.7
parent:      101578:55e6b4fd0e04
user:        Martin Panter <vadmium+py at gmail.com>
date:        Wed Jun 01 08:20:22 2016 +0000
summary:
  Issue #25570: Add example of customizing User-Agent via add_header()

files:
  Doc/howto/urllib2.rst   |  2 ++
  Doc/library/urllib2.rst |  7 ++++++-
  2 files changed, 8 insertions(+), 1 deletions(-)


diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
--- a/Doc/howto/urllib2.rst
+++ b/Doc/howto/urllib2.rst
@@ -1,3 +1,5 @@
+.. _urllib-howto:
+
 ************************************************
   HOWTO Fetch Internet Resources Using urllib2
 ************************************************
diff --git a/Doc/library/urllib2.rst b/Doc/library/urllib2.rst
--- a/Doc/library/urllib2.rst
+++ b/Doc/library/urllib2.rst
@@ -167,7 +167,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 used by a browser to identify itself --
+   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
    (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"``, while :mod:`urllib2`'s
@@ -950,6 +950,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 100 bytes of
 it::
 
@@ -1016,6 +1019,8 @@
    import urllib2
    req = urllib2.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 = urllib2.urlopen(req)
 
 :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list