Use the httplib module to create an HTTP client in Python
This article mainly introduces how to use the httplib module to create an HTTP client in Python. It lists some common HTTP methods in httplib. For more information, see
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:
?
| 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 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. The following describes HTTPResponse.
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:
?
| 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", 80, 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' +'-'* 50 +'/N' # Print res. read () Conn. close () |
Here is the response header I tracked with firebug:
The Httplib module also defines many constants, such:
The value of Httplib. HTTP_PORT is 80, indicating that the default port number is 80;
The value of Httplib. OK is 200, indicating that the request is successfully returned;
The value of Httplib. NOT_FOUND is 404, indicating that the requested resource does not exist;
You can use httplib. responses to query the meanings of related variables, such:
Print httplib. responses [httplib. NOT_FOUND] # not found