Python Network Programming Connection Reuse
Python Network Programming: Connection Reuse
When a client makes a valid request to a server, a temporary connection is established between them to complete the sending and receiving process. However, in some cases, the connection needs to be kept alive because the communicating programs need to automatically request and respond to each other. Take an interactive web page as an example. After the page loads, form data needs to be submitted or further CSS and JavaScript components need to be downloaded. To improve performance, maintaining uninterrupted communication between the client and the server requires a keep-alive connection.
Python provides the urllib3 module, which has several methods to handle connection reuse between clients and servers. In the following example, we create a single connection and make multiple requests by passing different parameters in the GET request. We receive multiple responses, but we also count the number of connections used in the process. As we can see, the number of connections does not change, which means that connections are reused.
from urllib3 import HTTPConnectionPool
pool = HTTPConnectionPool('ajax.googleapis.com', maxsize=1)
r = pool.request('GET', '/ajax/services/search/web',
fields={'q': 'python', 'v': '1.0'})
print 'Response Status:', r.status
# Header of the response
print 'Header: ',r.headers['content-type']
# Content of the response
print 'Python: ',len(r.data)
r = pool.request('GET', '/ajax/services/search/web',
fields={'q': 'php', 'v': '1.0'})
# Content of the response
print 'php: ',len(r.data)
print 'Number of Connections: ',pool.num_connections
print 'Number of requests: ',pool.num_requests
When we run the above program, we get the following output −
Response Status: 200
Header: text/javascript; charset=utf-8
Python: 211
php: 211
Number of Connections: 1
Number of requests: 2