diff options
author | Fred Drake <fdrake@acm.org> | 2003-04-25 15:27:33 +0000 |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2003-04-25 15:27:33 +0000 |
commit | c4874233ab2770c4ad47cf5ef8c65945a6a20ec0 (patch) | |
tree | 265b59f677301c8778d48e808c73794de15d05ac /Doc/lib | |
parent | 78e8d6267185a1022b8e00278eff4e4734063cca (diff) | |
download | cpython-c4874233ab2770c4ad47cf5ef8c65945a6a20ec0.tar.gz |
Add modified versions of the examples from Sean Reifschneider
(SF patch #545480).
Diffstat (limited to 'Doc/lib')
-rw-r--r-- | Doc/lib/liburllib2.tex | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Doc/lib/liburllib2.tex b/Doc/lib/liburllib2.tex index 9f279dfabc..7b47f9b0a2 100644 --- a/Doc/lib/liburllib2.tex +++ b/Doc/lib/liburllib2.tex @@ -601,3 +601,39 @@ Open the gopher resource indicated by \var{req}. \begin{methoddesc}[UnknownHandler]{unknown_open}{} Raise a \exception{URLError} exception. \end{methoddesc} + + +\subsection{Examples \label{urllib2-examples}} + +This example gets the python.org main page and displays the first 100 +bytes of it: + +\begin{verbatim} +>>> import urllib2 +>>> f = urllib2.urlopen('http://www.python.org/') +>>> print f.read(100) +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<?xml-stylesheet href="./css/ht2html +\end{verbatim} + +Here we are sending a data-stream to the stdin of a CGI and reading +the data it returns to us: + +\begin{verbatim} +>>> import urllib2 +>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi', +... data='This data is passed to stdin of the CGI') +>>> f = urllib2.urlopen(req) +>>> print f.read() +Got Data: "This data is passed to stdin of the CGI" +\end{verbatim} + +The code for the sample CGI used in the above example is: + +\begin{verbatim} +#!/usr/bin/env python +import sys +data = sys.stdin.read() +print 'Content-type: text-plain\n\nGot Data: "%s"' % +data +\end{verbatim} |