Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7aaaded

Browse files
committed
Issue #25570: Add example of customizing User-Agent via add_header()
1 parent f9aff92 commit 7aaaded

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

Doc/library/urllib.request.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ The following classes are provided:
189189

190190
*headers* should be a dictionary, and will be treated as if
191191
:meth:`add_header` was called with each key and value as arguments.
192-
This is often used to "spoof" the ``User-Agent`` header, which is
192+
This is often used to "spoof" the ``User-Agent`` header value, which is
193193
used by a browser to identify itself -- some HTTP servers only
194194
allow requests coming from common browsers as opposed to scripts.
195195
For example, Mozilla Firefox may identify itself as ``"Mozilla/5.0
@@ -1111,6 +1111,9 @@ HTTPErrorProcessor Objects
11111111
Examples
11121112
--------
11131113

1114+
In addition to the examples below, more examples are given in
1115+
:ref:`urllib-howto`.
1116+
11141117
This example gets the python.org main page and displays the first 300 bytes of
11151118
it. ::
11161119

@@ -1220,6 +1223,8 @@ Use the *headers* argument to the :class:`Request` constructor, or::
12201223
import urllib.request
12211224
req = urllib.request.Request('http://www.example.com/')
12221225
req.add_header('Referer', 'http://www.python.org/')
1226+
# Customize the default User-Agent header value:
1227+
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
12231228
r = urllib.request.urlopen(req)
12241229

12251230
:class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to

0 commit comments

Comments
 (0)