Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a561c038
Commit
a561c038
authored
Nov 24, 2015
by
Martin Panter
Browse files
Options
Browse Files
Download
Plain Diff
Issue #25576: Merge www-form-urlencoded doc from 3.5
parents
33623b14
f7e8d075
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
30 deletions
+16
-30
Doc/howto/urllib2.rst
Doc/howto/urllib2.rst
+3
-3
Doc/library/urllib.parse.rst
Doc/library/urllib.parse.rst
+5
-4
Doc/library/urllib.request.rst
Doc/library/urllib.request.rst
+8
-23
No files found.
Doc/howto/urllib2.rst
View file @
a561c038
...
...
@@ -115,7 +115,7 @@ library. ::
'language' : 'Python' }
data = urllib.parse.urlencode(values)
data = data.encode('
utf-8
') # data should be bytes
data = data.encode('
ascii
') # data should be bytes
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
the_page = response.read()
...
...
@@ -180,8 +180,8 @@ Explorer [#]_. ::
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data
= urllib.parse.urlencode(values)
data = data.encode('
utf-8
')
data = urllib.parse.urlencode(values)
data = data.encode('
ascii
')
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as response:
the_page = response.read()
...
...
Doc/library/urllib.parse.rst
View file @
a561c038
...
...
@@ -535,10 +535,11 @@ task isn't already covered by the URL parsing functions above.
errors=None, quote_via=quote_plus)
Convert a mapping object or a sequence of two-element tuples, which may
contain :class:`str` or :class:`bytes` objects, to a "percent-encoded"
string. If the resultant string is to be used as a *data* for POST
operation with :func:`~urllib.request.urlopen` function, then it should be
properly encoded to bytes, otherwise it would result in a :exc:`TypeError`.
contain :class:`str` or :class:`bytes` objects, to a percent-encoded ASCII
text string. If the resultant string is to be used as a *data* for POST
operation with the :func:`~urllib.request.urlopen` function, then
it should be encoded to bytes, otherwise it would result in a
:exc:`TypeError`.
The resulting string is a series of ``key=value`` pairs separated by ``'&'``
characters, where both *key* and *value* are quoted using the *quote_via*
...
...
Doc/library/urllib.request.rst
View file @
a561c038
...
...
@@ -36,13 +36,8 @@ The :mod:`urllib.request` module defines the following functions:
*data* should be a buffer in the standard
:mimetype:`application/x-www-form-urlencoded` format. The
:func:`urllib.parse.urlencode` function takes a mapping or sequence of
2-tuples and returns a string in this format. It should be encoded to bytes
before being used as the *data* parameter. The charset parameter in
``Content-Type`` header may be used to specify the encoding. If charset
parameter is not sent with the Content-Type header, the server following the
HTTP 1.1 recommendation may assume that the data is encoded in ISO-8859-1
encoding. It is advisable to use charset parameter with encoding used in
``Content-Type`` header with the :class:`Request`.
2-tuples and returns an ASCII text string in this format. It should
be encoded to bytes before being used as the *data* parameter.
urllib.request module uses HTTP/1.1 and includes ``Connection:close`` header
in its HTTP requests.
...
...
@@ -180,16 +175,9 @@ The following classes are provided:
the only ones that use *data*; the HTTP request will be a POST instead of a
GET when the *data* parameter is provided. *data* should be a buffer in the
standard :mimetype:`application/x-www-form-urlencoded` format.
The :func:`urllib.parse.urlencode` function takes a mapping or sequence of
2-tuples and returns a string in this format. It should be encoded to bytes
before being used as the *data* parameter. The charset parameter in
``Content-Type`` header may be used to specify the encoding. If charset
parameter is not sent with the Content-Type header, the server following the
HTTP 1.1 recommendation may assume that the data is encoded in ISO-8859-1
encoding. It is advisable to use charset parameter with encoding used in
``Content-Type`` header with the :class:`Request`.
2-tuples and returns an ASCII string in this format. It should be
encoded to bytes before being used as the *data* parameter.
*headers* should be a dictionary, and will be treated as if
:meth:`add_header` was called with each key and value as arguments.
...
...
@@ -202,7 +190,7 @@ The following classes are provided:
``"Python-urllib/2.6"`` (on Python 2.6).
An example of using ``Content-Type`` header with *data* argument would be
sending a dictionary like ``{"Content-Type":
" application/x-www-form-urlencoded;charset=utf-8
"}``.
sending a dictionary like ``{"Content-Type":
"application/x-www-form-urlencoded
"}``.
The final two arguments are only of interest for correct handling
of third-party HTTP cookies:
...
...
@@ -1230,7 +1218,7 @@ every :class:`Request`. To change this::
opener.open('http://www.example.com/')
Also, remember that a few standard headers (:mailheader:`Content-Length`,
:mailheader:`Content-Type`
without charset parameter
and :mailheader:`Host`)
:mailheader:`Content-Type` and :mailheader:`Host`)
are added when the :class:`Request` is passed to :func:`urlopen` (or
:meth:`OpenerDirector.open`).
...
...
@@ -1253,11 +1241,8 @@ from urlencode is encoded to bytes before it is sent to urlopen as data::
>>> import urllib.request
>>> import urllib.parse
>>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> data = data.encode('utf-8')
>>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
>>> # adding charset parameter to the Content-Type header.
>>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
>>> with urllib.request.urlopen(request, data) as f:
>>> data = data.encode('ascii')
>>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
... print(f.read().decode('utf-8'))
...
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment