This article mainly introduces the use of Httplib module to make Python under the HTTP client method, the article listed some httplib under the common HTTP method, the need for friends can refer to the
Httplib is a client-side implementation of the HTTP protocol in Python that can be used to interact with HTTP servers. Httplib is not a lot of content, but also relatively simple. The following is a very simple example of using Httplib to get the HTML of the homepage of Google:
?
1 2 3 4 5 6 |
#coding =GBK Import Httplib conn = Httplib. Httpconnection ("www.google.cn") conn.request (' Get ', '/') print conn.getresponse (). Read () Conn.close () |
The following is a detailed description of the common types and methods provided by Httplib.
Httplib. Httpconnection (host [, Port [, strict [, timeout]]]
The constructor of the Httpconnection class that represents an interaction with the server, that is, the request/response. The parameter host represents the server host, such as: Www.csdn.net;port is the port number, and the default value is 80; The default value of the parameter strict is false, which indicates whether the Badstatusline exception is thrown when the status line (the more typical status lines such as http/1.0) is not resolved, and the optional parameter timeout indicates the timeout period.
Httpconnection provides the following methods:
Httpconnection.request (method, url [, body [, headers]])
Calling the request method sends a call to the server to represent the method of the request, often with a method that has get and post; The URL represents the URL of the requested resource; The body indicates that the data submitted to the server must be a string (if method is "post", You can interpret the body as data in an HTML form; Headers represents the HTTP header of the request.
Httpconnection.getresponse ()
Gets the HTTP response. The returned object is an instance of HttpResponse, as explained in HttpResponse 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 debugging information is exported.
Httplib. HttpResponse
HttpResponse represents the server's response to client requests. Often created by calling Httpconnection.getresponse (), it has the following methods and properties:
Httpresponse.read ([Amt])
Gets the message body of the response. If the request is for an ordinary Web page, the method returns the HTML of the page. Optional Parameter Amt represents the reading of the specified byte of data from the response stream.
Httpresponse.getheader (name[, default])
Gets the response header. Name represents the Header field (header field) name, and 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
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. Such as: 200 indicates a successful request.
Httpresponse.reason
Returns a description of the results of the server processing request. Generally "OK"
The following is an example to familiarize yourself with the methods in HttpResponse:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
#coding =GBK Import Httplib conn = Httplib. Httpconnection ("www.g.cn", "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 PR int ' reason: ', res.reason print ' status: ', Res.status print ' msg: ', res.msg print ' headers: ', res.getheaders () #html #prin T '/n ' + '-' * + '/n ' #print res.read () conn.close () |
This is where I tracked the response head with Firebug:
Many constants are also defined in the Httplib module, such as:
Httplib. The value of Http_port is 80, which indicates that the default port number is 80;
The value of Httplib.ok is 200, which indicates a successful return of the request;
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