cpython: 6d5336a193cc (original) (raw)

Mercurial > cpython

changeset 95555:6d5336a193cc 3.4

Issue #12955: Change the urlopen() examples to use context managers where appropriate. Patch by Martin Panter. [#12955]

Berker Peksag berker.peksag@gmail.com
date Sun, 12 Apr 2015 13:52:49 +0300
parents 7463c06f6e87
children 08adaaf08697 c6dc1e0db7f0
files Doc/faq/library.rst Doc/howto/urllib2.rst Doc/library/concurrent.futures.rst Doc/library/urllib.request.rst Doc/tutorial/stdlib.rst
diffstat 5 files changed, 38 insertions(+), 28 deletions(-)[+] [-] Doc/faq/library.rst 3 Doc/howto/urllib2.rst 16 Doc/library/concurrent.futures.rst 4 Doc/library/urllib.request.rst 34 Doc/tutorial/stdlib.rst 9

line wrap: on

line diff

--- a/Doc/faq/library.rst +++ b/Doc/faq/library.rst @@ -687,7 +687,8 @@ Yes. Here's a simple example that uses u ### connect and send the server a path req = urllib.request.urlopen('http://www.some-server.out-there'[](#l1.5) '/cgi-bin/some-cgi-script', data=qs)

Note that in general for percent-encoded POST operations, query strings must be quoted using :func:urllib.parse.urlencode. For example, to send

--- a/Doc/howto/urllib2.rst +++ b/Doc/howto/urllib2.rst @@ -53,8 +53,8 @@ Fetching URLs The simplest way to use urllib.request is as follows:: import urllib.request

If you wish to retrieve a resource via URL and store it in a temporary location, you can do so via the :func:~urllib.request.urlretrieve function:: @@ -79,8 +79,8 @@ response:: import urllib.request req = urllib.request.Request('http://www.voidspace.org.uk')[](#l2.17)

Note that urllib.request makes use of the same Request interface to handle all URL schemes. For example, you can make an FTP request like so:: @@ -117,8 +117,8 @@ library. :: data = urllib.parse.urlencode(values) data = data.encode('utf-8') # data should be bytes req = urllib.request.Request(url, data)

Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see `HTML Specification, Form Submission @@ -183,8 +183,8 @@ Explorer [#]_. :: data = urllib.parse.urlencode(values) data = data.encode('utf-8') req = urllib.request.Request(url, data, headers)

The response also has two useful methods. See the section on info and geturl_ which comes after we have a look at what happens when things go wrong.

--- a/Doc/library/concurrent.futures.rst +++ b/Doc/library/concurrent.futures.rst @@ -138,8 +138,8 @@ ThreadPoolExecutor Example # Retrieve a single page and report the url and contents def load_url(url, timeout):

# We can use a with statement to ensure threads are cleaned up promptly with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:

--- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1048,8 +1048,9 @@ This example gets the python.org main pa it. :: >>> import urllib.request

The code for the sample CGI used in the above example is:: @@ -1107,7 +1109,8 @@ Here is an example of doing a PUT re import urllib.request DATA=b'some data' req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')

The following example uses the POST method instead. Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data:: @@ -1186,8 +1191,9 @@ from urlencode is encoded to bytes befor >>> request = urllib.request.Request("http://requestb.in/xrbl82xr")[](#l4.51) >>> # adding charset parameter to the Content-Type header. >>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")

The following example uses an explicitly specified HTTP proxy, overriding environment settings:: @@ -1195,15 +1201,17 @@ environment settings:: >>> import urllib.request >>> proxies = {'http': 'http://proxy.example.com:8080/'}[](#l4.64) >>> opener = urllib.request.FancyURLopener(proxies)

The following example uses no proxies at all, overriding environment settings:: >>> import urllib.request >>> opener = urllib.request.FancyURLopener({})

Legacy interface

--- a/Doc/tutorial/stdlib.rst +++ b/Doc/tutorial/stdlib.rst @@ -153,10 +153,11 @@ protocols. Two of the simplest are :mod: from URLs and :mod:smtplib for sending mail:: >>> from urllib.request import urlopen