Python Httplib Module Use instance _python

Source: Internet
Author: User
Tags ranges

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.