Network Programming in Python : The Basic, A Detailed Guide to Python 3 Network Programming and Management by John Galbraith

Network Programming in Python : The Basic, A Detailed Guide to Python 3 Network Programming and Management by John Galbraith

Author:John Galbraith [Galbraith, John]
Language: eng
Format: epub
Publisher: BPB Publications
Published: 2022-08-15T00:00:00+00:00


Keep-Alive, Connections, and httplib

If a connection is already open, the three-way handshake that initiates a TCP connection (see Chapter 3) can be skipped, which gave the impetus for HTTP to allow connections to stay open as a browser received an HTTP resource, then its JavaScript, and finally its CSS and images. The cost of establishing up a new connection has increased due to the development of TLS (see Chapter 6) as a best practise for all HTTP connections, increasing the value of connection reuse.

It is now the default for an HTTP connection to stay open after a request in protocol version HTTP/1.1. Connection: can be specified by either the client or the server. If they intend to hang up once a request is completed, close the connection; otherwise, a single TCP connection can be used to fetch as many resources from the server as the client desires. Web browsers frequently establish four or more simultaneous TCP connections per site in order to download a page and all of its supporting data and pictures in simultaneously in order to get them in front of the user as rapidly as feasible. If you are an implementer who is interested in the details, you should examine Section 6 of RFC 7230 to learn about the comprehensive connection control mechanism.

The urllib module does not allow for connection reuse, which is disappointing. Only the lower-level httplib module in the Standard Library allows you to make two requests on the same connection.

>>> import http.client

>>> h = http.client.HTTPConnection(‘localhost:8000’)

>>> h.request(‘GET’, ‘/ip’)

>>> r = h.getresponse()

>>> r.status

200

>>> h.request(‘GET’, ‘/user-agent’)

>>> r = h.getresponse()

>>> r.status

200

When you ask it to conduct another request, an HTTPConnection object that has become stuck will not return an error, but it will discreetly construct a new TCP connection to replace the previous one. A TLS-protected version of the same object is provided by the HTTPSConnection class. The Session object in the Requests library, on the other hand, is supported by urllib3, a third-party package that keeps track of open connections to HTTP servers with which you’ve recently communicated so that it can try to reuse them automatically when you ask for another resource from the same site.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.