Python httplib module instance, pythonhttplib

Source: Internet
Author: User

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

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.