mirror of
https://github.com/python/cpython.git
synced 2025-11-01 10:45:30 +00:00
Skip Montanaro <skip@mojam.com>:
Added an example of using an HTTP POST request.
This commit is contained in:
parent
fa48116993
commit
4e716fa0ac
1 changed files with 18 additions and 1 deletions
|
|
@ -114,7 +114,7 @@ read, using the \method{read()}, \method{readline()} or
|
|||
\subsection{Example}
|
||||
\nodename{HTTP Example}
|
||||
|
||||
Here is an example session:
|
||||
Here is an example session that uses the \samp{GET} method:
|
||||
|
||||
\begin{verbatim}
|
||||
>>> import httplib
|
||||
|
|
@ -129,3 +129,20 @@ Here is an example session:
|
|||
>>> data = f.read() # Get the raw HTML
|
||||
>>> f.close()
|
||||
\end{verbatim}
|
||||
|
||||
Here is an example session that shows how to \samp{POST} requests:
|
||||
|
||||
\begin{verbatim}
|
||||
>>> import httplib, urllib
|
||||
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
|
||||
>>> h = httplib.HTTP("www.musi-cal.com:80")
|
||||
>>> h.putrequest("POST", "/cgi-bin/query")
|
||||
>>> h.putheader("Content-length", "%d" % len(params))
|
||||
>>> h.putheader('Accept', 'text/plain')
|
||||
>>> h.putheader('Host', 'www.musi-cal.com')
|
||||
>>> h.endheaders()
|
||||
>>> h.send(paramstring)
|
||||
>>> reply, msg, hdrs = h.getreply()
|
||||
>>> print errcode # should be 200
|
||||
>>> data = h.getfile().read() # get the raw HTML
|
||||
\end{verbatim}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue