@@ -1235,8 +1235,9 @@ class ResponseReader(io.RawIOBase):
1235
1235
# For testing, you can use a StringIO as the argument to
1236
1236
# ``ResponseReader`` instead of an ``httplib.HTTPResponse``. It
1237
1237
# will work equally well.
1238
- def __init__ (self , response ):
1238
+ def __init__ (self , response , connection = None ):
1239
1239
self ._response = response
1240
+ self ._connection = connection
1240
1241
self ._buffer = ''
1241
1242
1242
1243
def __str__ (self ):
@@ -1262,6 +1263,8 @@ def peek(self, size):
1262
1263
1263
1264
def close (self ):
1264
1265
"""Closes this response."""
1266
+ if _connection :
1267
+ _connection .close ()
1265
1268
self ._response .close ()
1266
1269
1267
1270
def read (self , size = None ):
@@ -1332,25 +1335,29 @@ def request(url, message, **kwargs):
1332
1335
"Host" : host ,
1333
1336
"User-Agent" : "splunk-sdk-python/1.5.0" ,
1334
1337
"Accept" : "*/*" ,
1338
+ "Connection" : "Close" ,
1335
1339
} # defaults
1336
1340
for key , value in message ["headers" ]:
1337
1341
head [key ] = value
1338
1342
method = message .get ("method" , "GET" )
1339
1343
1340
1344
connection = connect (scheme , host , port )
1345
+ is_keepalive = False
1341
1346
try :
1342
1347
connection .request (method , path , body , head )
1343
1348
if timeout is not None :
1344
1349
connection .sock .settimeout (timeout )
1345
1350
response = connection .getresponse ()
1351
+ is_keepalive = "keep-alive" in response .getheader ("connection" , default = "close" ).lower ()
1346
1352
finally :
1347
- connection .close ()
1353
+ if not is_keepalive :
1354
+ connection .close ()
1348
1355
1349
1356
return {
1350
1357
"status" : response .status ,
1351
1358
"reason" : response .reason ,
1352
1359
"headers" : response .getheaders (),
1353
- "body" : ResponseReader (response ),
1360
+ "body" : ResponseReader (response , connection if is_keepalive else None ),
1354
1361
}
1355
1362
1356
1363
return request
0 commit comments