Building an HTTP Client with httplib

Python provides a series of modules to create an HTTP client. The modules that Python provides in the standard library are httplib, urllib, and urllib2. These modules have different capabilities among all of them, but they are useful for most of your web tests. We can also find httplib packages and requests that provide some improvements over the standard httplib module.

This module defines a class that implements the HTTPConnection class.

The class accepts a host and a port as parameters. The host is required and the port is optional. An instance of this class represents a transaction with an HTTP server. It must be instantiated by passing a server identifier and an optional port number. If the port number is not specified, the port number of the server-identification string is extracted if it has the form host: port, otherwise the default HTTP port (80) is used.

You can find the following code in the request_httplib.py file:

import httplib

connection = httplib.HTTPConnection("www.packtpub.com")
connection.request("GET", "/networking-and-servers/mastering-python-networking-and-security")
response = connection.getresponse()
print response
print response.status, response.reason
data = response.read()
print data
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.13.192