Python--httplib module uses __python

Source: Internet
Author: User

Httplib is a relatively low-level HTTP request module, which has a special packaging module, such as Urllib built-in modules, Goto and other third-party modules, but the higher the encapsulation of the more inflexible, such as the Urllib module in the request error will not return the content of the results page, only the header information, For certain scenarios that require the return value of the error request to be detected, this module is not applicable.


1, Class Httplib. Httpconnection
Description
This class is used to create a request link for an HTTP type

Prototype:
Httpconnection (host[, port[, strict[, timeout]])
Host: Requested server host, cannot start with http://
Port: Server Web Service ports
Strict: Whether the requested status line is strictly checked, that is, the line of the http1.0/1.1 protocol version, that is, the first line of the request, the default is False, and when true, the error throws an exception
Timeout: timeout for single request, no time default to use global timeout in httplib module

Example:
conn1 = httpconnection (' www.baidu.com:80 ')
conn2 = httpconnection (' www.baidu.com ',)
conn3 = Httpconnection (' www.baidu.com ', 80,true,10)
Error instance:
conn3 = httpconnection (' www.baidu.com:80 ', true,10)

Return:
Httpconnection class will instantiate and return a Httpconnection object


2, Class Httplib. Httpsconnection
Description
This class is used to create a request link of HTTPS type

Prototype:
Httpsconnection (host[, port[, key_file[, cert_file[, strict[, timeout]]]
Key_file: A private key file containing the PEM format
Cert_file: A certified file containing the PEM format
Others: Other same HTTP parameters

Instance:
CONN3 = httpsconnection (' accounts.google.com ', 443,key_file,cert_file,true,10)

Return:
Also returns a Httpsconnection object

Attention:
To create an HTTPS link, you must ensure that the underlying socket module is a compilation mode that supports SSL, that is, the switch to the compile-time SSL option is open


3, Httpconnection Object Request method:
Description
Send a request

Prototype:
Conn.request (method, url[, body[, headers])
Method: The requested way, such as ' Get ', ' POST ', ' head ', ' put ', ' DELETE ', etc.
URL: The Web page path of the request. such as: '/index.html '
Body: Request whether to bring data, this parameter is a dictionary
Headers: Whether the request takes the lead message, the parameter is a dictionary, but the name of the key is the specified HTTP header keyword

Instance:
Conn.request (' Get ', '/', ', {' user-agent ': ' Test '})

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


4, Httpconnection object GetResponse method
Description
Gets an HTTP response object that is equivalent to executing the last 2 carriage returns

Prototype/instance:
res = Conn.getresponse ()

Return:
HttpResponse objects


5, Httpconnection object Close () method
Description
Closes the specified Httpconnect link

Instance:
Conn.close ()


6. HttpResponse Object Read method
Description
Get the content part of HTTP response, that is, Web page source

Prototype:
BODY = Res.read ([AMT])
AMT: Reads a specified length of characters, the default is null, that is, read all content

Instance:
BODY = Res.read ()
pbody = Res.read (10)

Return:
Web page Content string


7. Other methods or properties of the HttpResponse object
Method:
Getheaders ()
Get all the response header content, is a tuple list [(Name,value), (name2,value2)]
GetHeader (Name[,default])
Get the specified header content
Fileno ()
The Fileno of the socket

Property:
Msg
All header information, just like the Getheaders method, except this is the original unhandled string
Status
When the status of the secondary request
Version
When the HTTP protocol version is requested, 10 is http1.0, 11 is http/1.1
Reason
When the result of the second request is expressed, 200 is ok,404 is not Found


Overall Example:
#!/usr/bin/env python  
#-*-coding:utf-8-*-  
import httplib
import 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__ ':  
    

Of course, there are other information, such as the exception type, such as the HTTP Code table and query dictionary, and so on, you can directly refer to the official website httplib documents: 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.