~ubuntu-branches/ubuntu/precise/python3.2/precise

« back to all changes in this revision

Viewing changes to Doc/library/urllib.request.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-02-14 16:12:14 UTC
  • mfrom: (10.1.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20110214161214-f5vwa226kebccmt9
Tags: 3.2~rc3-1
Python 3.2 release candidate 3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
967
967
 
968
968
   >>> import urllib.request
969
969
   >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
970
 
   ...                       data='This data is passed to stdin of the CGI')
 
970
   ...                       data=b'This data is passed to stdin of the CGI')
971
971
   >>> f = urllib.request.urlopen(req)
972
972
   >>> print(f.read().decode('utf-8'))
973
973
   Got Data: "This data is passed to stdin of the CGI"
1043
1043
   >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
1044
1044
   >>> print(f.read().decode('utf-8'))
1045
1045
 
1046
 
The following example uses the ``POST`` method instead::
 
1046
The following example uses the ``POST`` method instead. Note that params output
 
1047
from urlencode is encoded to bytes before it is sent to urlopen as data::
1047
1048
 
1048
1049
   >>> import urllib.request
1049
1050
   >>> import urllib.parse
1050
1051
   >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
 
1052
   >>> params = params.encode('utf-8')
1051
1053
   >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
1052
1054
   >>> print(f.read().decode('utf-8'))
1053
1055