Multiple requests in one connection?

Is it possible to place multiple requests without breaking the connection using python httplib ?. For example, can I upload a large file to the server in parts, but in one socket connection.

I was looking for answers. But nothing seemed so clear and definite.

Any examples / related links would be helpful. Thank you

+3
source share
2 answers

Yes, the connection remains open until you close it using the method close().

The following example, taken from the httplib documentation , shows how to execute multiple requests using the same connection:

>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> conn.request("GET", "/index.html")
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> data1 = r1.read()
>>> conn.request("GET", "/parrot.spam")
>>> r2 = conn.getresponse()
>>> print r2.status, r2.reason
404 Not Found
>>> data2 = r2.read()
>>> conn.close()
+11
source

.read() . :

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    conn.request("GET", "/2.html")
  File "C:\Python27\lib\httplib.py", line 955, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 983, in _send_request
    self.putrequest(method, url, **skips)
  File "C:\Python27\lib\httplib.py", line 853, in putrequest
    raise CannotSendRequest()
CannotSendRequest

, ( HTTP [, 404]).

+2

Source: https://habr.com/ru/post/1725698/


All Articles