Httplib module is an underlying basic module, which realizes less function and less use under normal circumstances. Recommended Urllib, URLLIB2, Httplib2.
Httpconnection Object
Class Httplib. Httpconnection (host[, port[, strict[, timeout[, Source_address]])
Create a Httpconnection object
Httpconnection.request (method, url[, body[, headers])
Send Request
Httpconnection.getresponse ()
Get a response
HttpResponse Object
Httpresponse.read ([Amt])
Reads and returns the response body, or up to the next Amt bytes.
Httpresponse.getheader (name[, default])
Get the specified header information
Httpresponse.getheaders ()
Get a list of (header, value) tuples
Httpresponse.fileno ()
Get the underlying socket file descriptor
Httpresponse.msg
Get Header Content
Httpresponse.version
Get the header HTTP version
Httpresponse.status
Get the Return status code
Httpresponse.reason
Get return Instructions
Instance
The code is as follows:
#!/usr/bin/python
Import Httplib
conn = Httplib. Httpconnection ("www.bitsCN.com")
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:
The code is as follows:
OK
----------------------------------------
(' Content-length ', ' 106883 ')
(' accept-ranges ', ' bytes ')
(' Vary ', ' accept-encoding, accept-encoding ')
(' keep-alive ', ' timeout=20 ')
(' Server ', ' ngx_openresty ')
(' last-modified ', ' Fri, Apr 09:30:10 GMT ')
(' Connection ', ' keep-alive ')
(' ETag ', ' ' 55279822-1a183 ')
(' Date ', ' Fri, Apr 09:48:15 GMT ')
(' Content-type ', ' text/html; Charset=utf-8 ')
----------------------------------------
Server:ngx_openresty
Date:fri, APR 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, APR 09:30:10 GMT
Vary:accept-encoding
ETag: "55279822-1a183"
Accept-ranges:bytes