From 9a99f21f5fd0d7bc72e61eacd0dfc52840cf5fd5 Mon Sep 17 00:00:00 2001 From: liuchang0812 Date: Thu, 9 Feb 2017 10:34:02 +0800 Subject: [PATCH] close session --- qcloud_cos/cos_op.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/qcloud_cos/cos_op.py b/qcloud_cos/cos_op.py index df958ed..5a48a17 100644 --- a/qcloud_cos/cos_op.py +++ b/qcloud_cos/cos_op.py @@ -6,6 +6,7 @@ import json import hashlib import urllib +from contextlib import closing import cos_auth from cos_err import CosErr from cos_request import UploadFileRequest @@ -533,15 +534,15 @@ def _upload_slice_data(self, request, file_content, session, offset, retry=3): def __download_url(self, uri, filename): session = self._http_session - ret = session.get(uri, stream=True) - if ret.status_code in [200, 206]: - with open(filename, 'wb') as f: - for chunk in ret.iter_content(chunk_size=1024): - if chunk: - f.write(chunk) - f.flush() - else: - raise IOError("download failed " + ret.text) + with closing(session.get(uri, stream=True)) as ret: + if ret.status_code in [200, 206]: + with open(filename, 'wb') as f: + for chunk in ret.iter_content(chunk_size=1024): + if chunk: + f.write(chunk) + f.flush() + else: + raise IOError("download failed " + ret.text) def download_file(self, request): assert isinstance(request, DownloadFileRequest)