Httplib module is a low-level basic module, the implementation of a relatively small, under normal circumstances less use. Recommended Urllib, URLLIB2, Httplib2.
Httpconnection objects
Class Httplib. Httpconnection (host[, port[, strict[, timeout[, Source_address]]]
Creating Httpconnection Objects
Httpconnection.request (method, url[, body[, headers])
Send Request
Httpconnection.getresponse ()
Get a response
HttpResponse objects
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 first HTTP version
Httpresponse.status
Get Return status code
Httpresponse.reason
Get Return description
Instance
Copy Code code 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 code code 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 2015 09:30:10 GMT ')
(' Connection ', ' keep-alive ')
(' ETag ', ' "55279822-1a183")
(' Date ', ' Fri, APR 2015 09:48:15 GMT ')
(' Content-type ', ' text/html; Charset=utf-8 ')
----------------------------------------
Server:ngx_openresty
Date:fri, 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, APR 2015 09:30:10 GMT
Vary:accept-encoding
ETag: "55279822-1a183"
Accept-ranges:bytes