- Python Network Programming - Home
- Python Network Introduction
- Python Networking - Environment Setup
- Python Networking - Internet Protocol
- Python Networking - IP Address
- Python Networking - DNS Lookup
- Python Networking - Routing
- Python Networking - HTTP Requests
- Python Networking - HTTP Response
- Python Networking - HTTP Headers
- Python Networking - Custom HTTP Requests
- Python Networking - Request Status Codes
- Python Networking - HTTP Authentication
- Python Networking - HTTP Data Download
- Python Networking - Connection Re-use
- Python Networking - Network Interface
- Python Networking - Sockets Programming
- Python Networking - HTTP Client
- Python Networking - HTTP Server
- Python Networking - Building URLs
- Python Networking - WebForm Submission
- Python Networking - Databases and SQL
- Python Networking - Telnet
- Python Networking - Email Messages
- Python Networking - SMTP
- Python Networking - POP3
- Python Networking - IMAP
- Python Networking - SSH
- Python Networking - FTP
- Python Networking - SFTP
- Python Networking - Web Servers
- Python Networking - Uploading Data
- Python Networking - Proxy Server
- Python Networking - Directory Listing
- Python Networking - Remote Procedure Call
- Python Networking - RPC JSON Server
- Python Networking - Google Maps
- Python Networking - RSS Feed
Python Network Programming Resources
Python Network - Connection Re-Use
When a client makes a valid request to a server, a temporary connection is established between them to complete the sending and receiving process. But there are scenarios where the connection needs to be kept alive as there is need of automatic requests and responses between the programs which are communicating. Take for example an interactive webpage. After the webpage is loaded there is a need of submitting a form data or downloading further CSS and JavaScript components. The connection needs to be kept alive for faster performance and an unbroken communication between the client and the server.
Python provides urllib3 module which had methods to take care of connection reuse between a client and a server. In the below example we create a connection and make multiple requests by passing different parameters with the GET request. We receive multiple responses but we also count the number of connection that has been used in the process. As we see the number of connection does not change implying the reuse of the connection.
Example - Reusing Connections
main.py
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)
Output
When we run the above program, we get the following output −
Response Status: 404 Header: text/html; charset=UTF-8 Python: 1585 php: 1585 Number of Connections: 1 Number of requests: 2