Python httplib module instance, pythonhttplib
The httplib module is an underlying basic module that provides fewer functions and is rarely used under normal circumstances. We recommend using urllib, urllib2, and httplib2.
HTTPConnection object
Class httplib. HTTPConnection (host [, port [, strict [, timeout [, source_address])
Create an HTTPConnection object
HTTPConnection. request (method, url [, body [, headers])
Send request
HTTPConnection. getresponse ()
Get Response
HTTPResponse object
HTTPResponse. read ([amt])
Reads and returns the response body, or up to the next amt bytes.
HTTPResponse. getheader (name [, default])
Obtains the specified header information.
HTTPResponse. getheaders ()
Obtains the list of (header, value) tuples.
HTTPResponse. fileno ()
Obtain the underlying socket file descriptor
HTTPResponse. msg
GET header content
HTTPResponse. version
Obtain the http header version
HTTPResponse. status
Returns the status code.
HTTPResponse. reason
Get Response instructions
Instance
Copy codeThe Code is as follows:
#! /Usr/bin/python
Import httplib
Conn = httplib. HTTPConnection ("www.jb51.net ")
Conn. request ("GET ","/")
R1 = conn. getresponse ()
Print r1.status, r1.reason
Print '-' * 40
Headers = r1.getheaders ()
For h in headers:
Print h
Print '-' * 40
Print r1.msg
Output:
Copy codeThe Code is as follows:
200 OK
----------------------------------------
('Content-length', '123 ')
('Accept-ranges', 'bytes ')
('Vary ', 'Accept-Encoding, Accept-encoding ')
('Keep-alive', 'timeout = 20 ')
('Server', 'ngx _ openresty ')
('Last-modified', 'fri, 10 Apr 2015 09:30:10 gmt ')
('Connection', 'keep-alive ')
('Etag', '"55279822-1a183 "')
('Date', 'fri, 10 Apr 2015 09:48:15 gmt ')
('Content-type', 'text/html; charset = UTF-8 ')
----------------------------------------
Server: ngx_openresty
Date: Fri, 10 Apr 2015 09:48:15 GMT
Content-Type: text/html; charset = UTF-8
Content-Length: 106883
Connection: keep-alive
Keep-Alive: timeout = 20
Vary: Accept-Encoding
Last-Modified: Fri, 10 Apr 2015 09:30:10 GMT
Vary: Accept-Encoding
ETag: "55279822-1a183"
Accept-Ranges: bytes