Python module learning-httplib HTTP client implementation

Source: Internet
Author: User
Document directory
  • Httpconnection. Request (method, URL [, body [, headers])
  • Httpconnection. getresponse ()
  • Httpconnection. Connect ()
  • Httpconnection. Close ()
  • Httpconnection. set_debuglevel (level)
  • Httpresponse. Read ([AMT])
  • Httpresponse. getheader (name [, default])
  • Httpresponse. getheaders ()
  • Httpresponse. msg
  • Httpresponse. Version
  • Httpresponse. Status
  • Httpresponse. Reason

Httplib is the HTTP client implementation in Python. It can be used to interact with the HTTP server. Httplib does not have a lot of content and is also relatively simple. The following is a simple example: Use httplib to obtain the Google homepage HTML:
# Coding = GBK <br/> Import httplib <br/> conn = httplib. httpconnection ("www.google.cn") <br/> Conn. request ('get', '/') <br/> Print Conn. getresponse (). read () <br/> Conn. close ()

The following describes the common types and methods provided by httplib.

Httplib. httpconnection (host [, Port [, strict [, timeout])

The constructor of the httpconnection class, indicating an interaction with the server, that is, a request/response. The host parameter indicates the server host, for example, www.csdn.net; port indicates the port number and the default value is 80; the default value of the strict parameter is false, indicating that the status line returned by the server cannot be parsed) (typical status rows such as HTTP/1.0 200 OK): whether to throw a badstatusline exception. The optional parameter timeout indicates the timeout time.

Methods provided by httpconnection:

Httpconnection. Request (method, URL [, body [, headers])

When the request method is called, a request is sent to the server. The method indicates the request method. Common methods include get and post. The URL indicates the URL of the requested resource. The body indicates the data submitted to the server, it must be a string (if the method is "Post", the body can be understood as the data in the HTML form); headers indicates the HTTP header of the request.

Httpconnection. getresponse ()

Obtain the HTTP response. The returned object is an instance of httpresponse.
Will explain.

Httpconnection. Connect ()

Connect to the HTTP server.

Httpconnection. Close ()

Close the connection to the server.

Httpconnection. set_debuglevel (level)

Set the height level. The default value of the parameter level is 0, indicating that no debugging information is output.


Httplib. httpresponse

Httpresponse indicates the server's response to the client request. It is often created by calling httpconnection. getresponse (). It has the following methods and attributes:

Httpresponse. Read ([AMT])

Obtain the Response Message Body. If the request is a common webpage, the method returns the HTML of the webpage. The optional parameter AMT indicates that the specified bytes of data are read from the response stream.

Httpresponse. getheader (name [, default])

Obtain the response header. Name indicates the header field name. The optional parameter default is returned as the default value if the header domain name does not exist.

Httpresponse. getheaders ()

Returns all header information in the form of a list.

Httpresponse. msg

Obtain all response header information.

Httpresponse. Version

Obtain the HTTP protocol version used by the server. 11 indicates HTTP/1.1; 10 indicates HTTP/1.0.

Httpresponse. Status

Obtain the status code of the response. For example, 200 indicates that the request is successful.

Httpresponse. Reason

Returns the description of the server processing request. Generally "OK"

The following is an example to familiarize yourself with the methods in httpresponse:

# Coding = GBK <br/> Import httplib <br/> conn = httplib. httpconnection ("www.g.cn", 80, false) <br/> Conn. request ('get', '/', headers = {"host": "www.google.cn", <br/> "User-Agent": "Mozilla/5.0 (windows; U; windows NT 5.1; ZH-CN; RV: 1.9.1) Gecko/20090624 Firefox/3.5 ", <br/>" accept ":" text/plain "}) <br/> res = Conn. getresponse () <br/> Print 'version: ', Res. version <br/> Print 'reason: ', Res. reason <br/> Print 'status: ', Res. status <br/> Print 'msg: ', Res. MSG <br/> Print 'headers: ', Res. getheaders () <br/> # HTML <br/> # print '/N' +'-'* 50 +'/N' <br/> # print res. read () <br/> Conn. close ()

Here is the response header I tracked with firebug:

 

The httplib module also defines many constants, such:
Httplib. http_port

The default port number is 80;
Httplib. OK

The value is 200, indicating that the request is successfully returned;
Httplib. not_found

The value is 404, indicating that the requested resource does not exist;

You can useHttplib. Responses

Query the meanings of related variables, such:

Print httplib. Responses [httplib. not_found] # not found

 

For more information about httplib, refer to the python manual.
Module.

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.