From f397219c2700298f61fde9158156126135a6094e Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Wed, 22 Jul 2015 22:21:28 -0500 Subject: [PATCH] Fix a file handle leak --- server.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/server.py b/server.py index 1288a53..c2457ff 100755 --- a/server.py +++ b/server.py @@ -103,13 +103,12 @@ class HttpHandler(BaseHTTPRequestHandler): filename = os.path.join(config['storage_path'], path) os.makedirs(os.path.dirname(filename)) remaining = length - f = open(filename,'wb') - data = self.rfile.read(4096) - while data and remaining >= 0: - remaining -= len(data) - f.write(data) - data = self.rfile.read(min(4096,remaining)) - f.close() + with open(filename,'wb') as f: + data = self.rfile.read(4096) + while data and remaining >= 0: + remaining -= len(data) + f.write(data) + data = self.rfile.read(min(4096,remaining)) self.send_response(200,'ok') self.end_headers() else: