GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.python.org — 20.7. httplib — HTTP protocol client — Python 2.7.18 documentation
This document is for an old version of Python that is no longer supported.
You should upgrade and read the
Python documentation for the current stable release.
20.7.
|
Constant |
Value |
Definition |
|---|---|---|
|
|
HTTP/1.1, RFC 2616, Section 10.1.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.1.2 |
|
|
WEBDAV, RFC 2518, Section 10.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.2 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.3 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.4 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.5 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.6 |
|
|
HTTP/1.1, RFC 2616, Section 10.2.7 |
|
|
WEBDAV RFC 2518, Section 10.2 |
|
|
Delta encoding in HTTP, RFC 3229, Section 10.4.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.2 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.3 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.4 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.5 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.6 |
|
|
HTTP/1.1, RFC 2616, Section 10.3.8 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.2 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.3 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.4 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.5 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.6 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.7 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.8 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.9 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.10 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.11 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.12 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.13 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.14 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.15 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.16 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.17 |
|
|
HTTP/1.1, RFC 2616, Section 10.4.18 |
|
|
WEBDAV, RFC 2518, Section 10.3 |
|
|
WEBDAV RFC 2518, Section 10.4 |
|
|
WEBDAV, RFC 2518, Section 10.5 |
|
|
HTTP Upgrade to TLS, RFC 2817, Section 6 |
|
|
HTTP/1.1, RFC 2616, Section 10.5.1 |
|
|
HTTP/1.1, RFC 2616, Section 10.5.2 |
|
|
HTTP/1.1 RFC 2616, Section 10.5.3 |
|
|
HTTP/1.1, RFC 2616, Section 10.5.4 |
|
|
HTTP/1.1 RFC 2616, Section 10.5.5 |
|
|
HTTP/1.1, RFC 2616, Section 10.5.6 |
|
|
WEBDAV, RFC 2518, Section 10.6 |
|
|
An HTTP Extension Framework, RFC 2774, Section 7 |
httplib.responses¶This dictionary maps the HTTP 1.1 status codes to the W3C names.
Example: httplib.responses[httplib.NOT_FOUND] is 'Not Found'.
New in version 2.5.
HTTPConnection instances have the following methods:
HTTPConnection.request(method, url[, body[, headers]])¶This will send a request to the server using the HTTP request method method
and the selector url. If the body argument is present, it should be a
string of data to send after the headers are finished. Alternatively, it may
be an open file object, in which case the contents of the file is sent; this
file object should support fileno() and read() methods. The
headers argument should be a mapping of extra HTTP headers to send with
the request.
If one is not provided in headers, a Content-Length header is added
automatically for all methods if the length of the body can be determined,
either from the length of the str representation, or from the reported
size of the file on disk. If body is None the header is not set except
for methods that expect a body (PUT, POST, and PATCH) in which
case it is set to 0.
Changed in version 2.6: body can be a file object.
HTTPConnection.getresponse()¶Should be called after a request is sent to get the response from the server.
Returns an HTTPResponse instance.
Note
Note that you must have read the whole response before you can send a new request to the server.
HTTPConnection.set_debuglevel(level)¶Set the debugging level (the amount of debugging output printed). The default
debug level is 0, meaning no debugging output is printed.
HTTPConnection.set_tunnel(host, port=None, headers=None)¶Set the host and the port for HTTP Connect Tunnelling. Normally used when it is required to do HTTPS Connection through a proxy server.
The headers argument should be a mapping of extra HTTP headers to send with the CONNECT request.
New in version 2.7.
HTTPConnection.connect()¶Connect to the server specified when the object was created.
HTTPConnection.close()¶Close the connection to the server.
As an alternative to using the request() method described above, you can
also send your request step by step, by using the four functions below.
HTTPConnection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])¶This should be the first call after the connection to the server has been made.
It sends a line to the server consisting of the request string, the selector
string, and the HTTP version (HTTP/1.1). To disable automatic sending of
Host: or Accept-Encoding: headers (for example to accept additional
content encodings), specify skip_host or skip_accept_encoding with non-False
values.
Changed in version 2.4: skip_accept_encoding argument added.
HTTPConnection.putheader(header, argument[, ...])¶Send an RFC 822-style header to the server. It sends a line to the server consisting of the header, a colon and a space, and the first argument. If more arguments are given, continuation lines are sent, each consisting of a tab and an argument.
HTTPConnection.endheaders(message_body=None)¶Send a blank line to the server, signalling the end of the headers. The optional message_body argument can be used to pass a message body associated with the request. The message body will be sent in the same packet as the message headers if it is string, otherwise it is sent in a separate packet.
Changed in version 2.7: message_body was added.
HTTPConnection.send(data)¶Send data to the server. This should be used directly only after the
endheaders() method has been called and before getresponse() is
called.
HTTPResponse instances have the following methods and attributes:
HTTPResponse.read([amt])¶Reads and returns the response body, or up to the next amt bytes.
HTTPResponse.getheader(name[, default])¶Get the contents of the header name, or default if there is no matching header.
HTTPResponse.getheaders()¶Return a list of (header, value) tuples.
New in version 2.4.
HTTPResponse.fileno()¶Returns the fileno of the underlying socket.
HTTPResponse.msg¶A mimetools.Message instance containing the response headers.
HTTPResponse.version¶HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
HTTPResponse.status¶Status code returned by server.
HTTPResponse.reason¶Reason phrase returned by server.
Here is an example session that uses the GET method:
>>> import httplib >>> conn = httplib.HTTPSConnection("www.python.org") >>> conn.request("GET", "/") >>> r1 = conn.getresponse() >>> print r1.status, r1.reason 200 OK >>> data1 = r1.read() >>> conn.request("GET", "/") >>> r2 = conn.getresponse() >>> print r2.status, r2.reason 404 Not Found >>> data2 = r2.read() >>> conn.close()
Here is an example session that uses the HEAD method. Note that the
HEAD method never returns any data.
>>> import httplib >>> conn = httplib.HTTPSConnection("www.python.org") >>> conn.request("HEAD","/") >>> res = conn.getresponse() >>> print res.status, res.reason 200 OK >>> data = res.read() >>> print len(data) 0 >>> data == '' True
Here is an example session that shows how to POST requests:
>>> import httplib, urllib >>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'}) >>> headers = {"Content-type": "application/x-www-form-urlencoded", ... "Accept": "text/plain"} >>> conn = httplib.HTTPConnection("bugs.python.org") >>> conn.request("POST", "", params, headers) >>> response = conn.getresponse() >>> print response.status, response.reason 302 Found >>> data = response.read() >>> data 'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>' >>> conn.close()
Client side HTTP PUT requests are very similar to POST requests. The
difference lies only the server side where HTTP server will allow resources to
be created via PUT request. Here is an example session that shows how to do
PUT request using httplib:
>>> # This creates an HTTP message >>> # with the content of BODY as the enclosed representation >>> # for the resource http://localhost:8080/foobar ... >>> import httplib >>> BODY = "***filecontents***" >>> conn = httplib.HTTPConnection("localhost", 8080) >>> conn.request("PUT", "/file", BODY) >>> response = conn.getresponse() >>> print response.status, response.reason 200, OK