Python httplib Module Usage examples

Source: Internet
Author: User
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

  • 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.