Re:Re: why the connection set with “keep live” in urllib.request always set to be“closed, thanks

xuanwu348 xuanwu348 at 163.com
Tue Aug 18 21:33:08 EDT 2020


Thanks all,


I had met this problem was when I did restapi test.
I send a request by python script and the time processed by server very short, 
and another request send by postman, the time seems normal, 
I compare the diff between these two request, 
obvious difference is the connection, which in postman request was keep-alive and in python script's was closed.
So I try to found out if it was the cause.
And at last we found that not the reason triggered this problem, and the really reason lead the problem is the parameter I transfered in json body. 
 
and thanks for the links you provid, I will read into them.  


Best Regards



At 2020-08-19 02:35:42, "Barry Scott" <barry at barrys-emacs.org> wrote:





On 18 Aug 2020, at 02:16, xuanwu348 <xuanwu348 at 163.com> wrote:




This means I want to structure my request header with "Connection: keep alive" for the server which support connect alive,
But I checked the requeset header send by myself, the connection still closed, no affect by set


And I also found this in python document:
==============================
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

Open the URL url, which can be either a string or a Request object.

data must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details.

urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests.

===============================
Does it mean urllib.request don't support 'keep alive'?


The term in HTTP 1.1 is persistent connections. See https://tools.ietf.org/html/rfc2616#section-8.1


The server will assume persistent connections for the reasons documented.


Since you have seen requests sending "Connection: close" that is saying to the server that
persistent connection is not required. The server will send the response and close its end.


Sounds like you need to use another library to allow you to use the persistent connection.


If you know your way around TCP sockets and protocols this is not that hard to write the
code to do persistent connections for a client.


Maybe look at a library like twisted? Its got all the building blocks you need to handle
persistent connections.


Barry




Thanks







在 2020-08-18 04:51:03,"Barry" <barry at barrys-emacs.org> 写道:
>
>
>> On 17 Aug 2020, at 18:23, xuanwu348 <xuanwu348 at 163.com> wrote:
>> 
>> hi everyone
>> 
>> 
>> Good day, the code as below, I try to make a long connection by set the connection to "keep-alive", but actually it's not as expected. Why?  Thanks
>
>What this means is. Please web server keep the connect alive if you can.
>The web server is allowed to close the connection if it wants too.
>
>Barry
>
>> 
>> 
>> import urllib.request as req
>> headers = {"authorization": "Bearer {}".format(self.token),
>>                   "Content-Type": "multipart/form-data; boundary={}".format(self.boundary),
>>                   "Connection": "keep-alive"}
>> request = req.Request(event_uri, headers=headers, data=data.encode("utf-8"))
>> 
>> 
>> 
>> 
>> 
>> Accept-Encoding: identity
>> 
>> Content-Length: 705
>> 
>> Host: 10.10.1.114:9443
>> 
>> User-Agent: Python-urllib/3.8
>> 
>> Authorization: Bearer a1.BUiPZxxxxxxxxxxxxxxxxxxCQdH2c9uegml
>> 
>> Content-Type: multipart/form-data; boundary=--testtest--
>> 
>> Connection: close
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> Best Regards
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
>> 




 




More information about the Python-list mailing list