How to use the Httplib module to make an HTTP client under Python

Source: Internet
Author: User
Tags html form
Httplib is a client implementation of the HTTP protocol in Python that can be used to interact with the HTTP server. The content of Httplib is not many, also relatively simple. Here is a very simple example of using Httplib to get the HTML for the Google home page:

#coding =GBK Import Httplib conn = Httplib. Httpconnection ("www.google.cn") conn.request (' Get ', '/') print conn.getresponse (). Read () Conn.close ()

The common types and methods provided by Httplib are described in detail below.
Httplib. Httpconnection (host [, Port [, strict [, timeout]])

A constructor for the Httpconnection class that represents a single interaction with the server, that is, a request/response. The parameter host represents the server host, such as: Www.csdn.net;port is the port number, the default value is 80, and the default value of the parameter strict is false, indicating that the state line returned by the server cannot be resolved (status lines) (more typical state rows such as: http/ 1.0 OK), whether to throw badstatusline exception; optional parameter timeout indicates the time-out.
Httpconnection provides the method:
Httpconnection.request (method, url [, body [, headers]])

Invoking the request method will send the server the requested method, which means that the method used has get and post, the URL represents the requested resource's URL, and the body represents the data submitted to the server, which must be a string (if method is "post", The body can be interpreted as the data in the HTML form); Headers represents the HTTP header of the request.
Httpconnection.getresponse ()

Gets the HTTP response. The returned object is an instance of HttpResponse, and about HttpResponse is explained below.
Httpconnection.connect ()

Connect to the HTTP server.
Httpconnection.close ()

Close the connection to the server.
Httpconnection.set_debuglevel (Level)

Sets the level of height. The default value of the parameter level is 0, which means that no debug information is output.
Httplib. HttpResponse

HttpResponse represents the server's response to a client request. is often created by calling Httpconnection.getresponse (), which has the following methods and properties:
Httpresponse.read ([Amt])

Gets the message body of the response. If the request is a normal Web page, then the method returns the HTML of the page. Optional Parameters Amt represents the data from the response stream that reads the specified bytes.
Httpresponse.getheader (name[, default])

Gets the response header. Name indicates the header field name, and the optional parameter default is returned as the default if the header domain name does not exist.
Httpresponse.getheaders ()

Returns all header information in the form of a list.
Httpresponse.msg

Gets all the response header information.
Httpresponse.version

Gets the version of the HTTP protocol used by the server. 11 indicates that HTTP/1.1;10 represents http/1.0.
Httpresponse.status

Gets the status code of the response. Example: 200 indicates a successful request.
Httpresponse.reason

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

Here's an example to familiarize yourself with the methods in HttpResponse:

#coding =GBK Import Httplib conn = Httplib. Httpconnection ("www.g.cn", "a", "False") conn.request (' Get ', '/', headers = {"Host": "www.google.cn",                   "user-agent": " mozilla/5.0 (Windows; U Windows NT 5.1; ZH-CN; rv:1.9.1) gecko/20090624 firefox/3.5 ",                   " Accept ":" Text/plain "}) res = Conn.getresponse () print ' version: ', Res.version print ' Reason: ', res.reason print ' status: ', Res.status print ' msg: ', res.msg print ' headers: ', res.getheaders () #html #print '/n ' + '-' * + '/n ' #print res.read () conn.close ()

This is what I tracked to the response header with Firebug:

Many constants are also defined in the Httplib module, such as:

Httplib. The value of Http_port is 80, which means that the default port number is 80;

The value of Httplib.ok is 200, indicating that the request was successfully returned;

Httplib. The value of Not_found is 404, indicating that the requested resource does not exist;

You can query the meaning of related variables by httplib.responses, such as:

Print Httplib.responses[httplib. Not_found] #not FOUND

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