Use the python-httplib Module

Source: Internet
Author: User

Httplib is a relatively low-layer HTTP request module with dedicated packaging modules, such as urllib built-in modules and goto third-party modules. However, the higher the encapsulation, the less flexible it is, for example, in the urllib module, when a request error occurs, the content of the result page is not returned, and only the header information is returned. This module is not applicable to some scenarios where an error request return value needs to be detected.

1. Class httplib. httpconnection

Note:
This class is used to create an HTTP request link

Prototype:
Httpconnection (host [, Port [, strict [, timeout])
HOST: the requested server host, which cannot start with http: //
Port: Server Web Service port
Strict: whether to strictly check the Request status line, that is, the line of http1.0/1.1 protocol version, that is, the first line of the request. The default value is false. If the value is true, an error will be thrown.
Timeout: the timeout time of a single request. If not, the global timeout time in the httplib module is used by default.

 
Instance: conn1 = httpconnection ('www .baidu.com: 80') conn2 = httpconnection ('www .baidu.com ', 80) conn3 = httpconnection ('www .baidu.com', 80, true, 10) error instance: conn3 = httpconnection ('www .baidu.com: 80', true, 10)

Return Value:
The httpconnection class will instance and return an httpconnection object

2. Class httplib. httpsconnection
Note:
This class is used to create an HTTPS request link

Prototype:
Httpsconnection (host [, Port [, key_file [, cert_file [, strict [, timeout])
Key_file: a private key file containing PEM format
Cert_file: An authentication file that contains the PEM format
Other: Other parameters with the same HTTP parameters

Instance:

 
Conn3 = httpsconnection ('accounts .google.com ', 443, key_file, cert_file, true, 10)

Return Value:
Returns an httpsconnection object.

Note:
To create an HTTPS link, make sure that the underlying socket module supports the SSL compilation mode, that is, the SSL option is enabled during compilation.

3. httpconnection Object Request Method:
Note:
Send a request

Prototype:
Conn. Request (method, URL [, body [, headers])
Method: The request method, such as 'get', 'post', 'head', 'put', and 'delete '.
URL: the requested webpage path. For example, '/index.html'
Body: whether the request carries data. This parameter is a dictionary.
Headers: whether the request takes the lead. This parameter is a dictionary, but the key name is the specified HTTP header keyword.

Instance:

 
Conn. Request ('get', '/', '', {'user-agent': 'test '})

Return Value:
No return is actually relative to sending data to the service, but there is no final carriage return

4. getresponse method of the httpconnection object
Note:
Get an HTTP Response object, which is equivalent to executing the last two carriage returns

Prototype/instance:

 
Res = conn. getresponse ()

Return Value:
Httpresponse object

5. httpconnection object close () method
Note:
Disable the specified httpconnect Link

Instance:

 
Conn. Close ()

6. httpresponse object read Method
Note:
Obtain the content of the HTTP response, that is, the webpage source code.

Prototype:
Body = res. Read ([AMT])
AMT: read characters of the specified length. The default value is null, that is, read all content.

Instance:

 
Body = res. Read () pbody = res. Read (10)

Return Value:
Webpage content string

7. other methods or attributes of the httpresponse object
Method:
Getheaders ()
Obtain all the response header content, which is a list of tuples [(name, value), (name2, value2)]
Getheader (name [, default])
Obtains the specified header content.
Fileno ()
Fileno of socket

Attribute:
MSG
All header information is the same as the getheaders method, except that this is the original unprocessed string.
Status
Status of the current request
Version
For the HTTP protocol version of the request, 10 is http1.0 and 11 is HTTP/1.1.
Reason
When the content of the request result is expressed, 200 is OK, 404 is not found

Overall instance:

 
#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import httplibimport urllib def sendhttp (): Data = urllib. urlencode ({'@ number': 12524,' @ type': 'issue ',' @ action': 'Show '}) headers = {"Content-Type ": "Application/X-WWW-form-urlencoded", "accept": "text/plain"} conn = httplib. httpconnection ('bugs .python.org ') Conn. request ('post', '/', Data, headers) httpres = Conn. getresponse () print httpres. status print httpres. reason print httpres. read () If _ name _ = '_ main _': sendhttp ()

Of course there are some other information, such as the exception type, such as HTTP code corresponding table and query dictionary, etc., you can directly refer to the official website httplib documentation: http://docs.python.org/library/httplib.html

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.