Commit b959822d authored by Lucas Carvalho's avatar Lucas Carvalho

We must close the HTTP connection after calling the getresponse method.

Otherwise, all the connections would still alive and this is not a good
behavior.

Once we need to close the connections it is not possible to return the
content of getresponse method, the method read would return an empty
string instead of the correct data.

And also the data returned by shadir is a json content,
to reach a better usage of libnetworkcache the data returned must be
loaded by json library, in other words, it must be a list.
parent 3544a454
...@@ -87,13 +87,17 @@ class NetworkcacheClient(object): ...@@ -87,13 +87,17 @@ class NetworkcacheClient(object):
sha512sum = hashlib.sha512(file_content).hexdigest() sha512sum = hashlib.sha512(file_content).hexdigest()
path = os.path.join(self.shacache_path, sha512sum) path = os.path.join(self.shacache_path, sha512sum)
self.shacache_connection = httplib.HTTPConnection(self.shacache_host) shacache_connection = httplib.HTTPConnection(self.shacache_host)
self.shacache_connection.request('POST', path, file_content, try:
shacache_connection.request('POST', path, file_content,
self.shacache_header_dict) self.shacache_header_dict)
result = self.shacache_connection.getresponse() result = shacache_connection.getresponse()
data = result.read()
finally:
shacache_connection.close()
if result.status != 200: if result.status != 200:
return result raise UploadError('Failed to upload. Response code: %s. Response data: %s' % (result.status, data))
if directory_key is not None: if directory_key is not None:
path = os.path.join(self.shadir_path, directory_key) path = os.path.join(self.shadir_path, directory_key)
...@@ -111,23 +115,34 @@ class NetworkcacheClient(object): ...@@ -111,23 +115,34 @@ class NetworkcacheClient(object):
signature = "SIGNATURE" signature = "SIGNATURE"
data = [kw, signature] data = [kw, signature]
self.shadir_connection = httplib.HTTPConnection(self.shadir_host) shadir_connection = httplib.HTTPConnection(self.shadir_host)
self.shadir_connection.request('POST', path, json.dumps(data), try:
shadir_connection.request('POST', path, json.dumps(data),
self.shadir_header_dict) self.shadir_header_dict)
result = self.shadir_connection.getresponse() result = shadir_connection.getresponse()
return result data = result.read()
finally:
shadir_connection.close()
return json.loads(data)
def download(self, sha512sum): def download(self, sha512sum):
''' Download the file. ''' Download the file.
It uses http GET request method. It uses http GET request method.
''' '''
path_info = os.path.join(self.shacache_path, sha512sum) path_info = os.path.join(self.shacache_path, sha512sum)
self.shacache_connection = httplib.HTTPConnection(self.shacache_host) shacache_connection = httplib.HTTPConnection(self.shacache_host)
self.shacache_connection.request('GET', path_info, headers=self.shacache_header_dict) try:
result = self.shacache_connection.getresponse() shacache_connection.request('GET', path_info, headers=self.shacache_header_dict)
result = shacache_connection.getresponse()
file_content = result.read()
finally:
shacache_connection.close()
if result.status != 200: if result.status != 200:
raise FileNotFound(result.read()) raise FileNotFound(result.read())
return result
return file_content
def select(self, directory_key=None): def select(self, directory_key=None):
'''Download a file from shacache by selecting the entry in shadir '''Download a file from shacache by selecting the entry in shadir
...@@ -137,22 +152,18 @@ class NetworkcacheClient(object): ...@@ -137,22 +152,18 @@ class NetworkcacheClient(object):
if directory_key is not None: if directory_key is not None:
path_info = os.path.join(self.shadir_path, directory_key) path_info = os.path.join(self.shadir_path, directory_key)
self.shadir_connection = httplib.HTTPConnection(self.shadir_host) shadir_connection = httplib.HTTPConnection(self.shadir_host)
self.shadir_connection.request('GET', path_info, headers=self.shadir_header_dict) try:
result = self.shadir_connection.getresponse() shadir_connection.request('GET', path_info, headers=self.shadir_header_dict)
result = shadir_connection.getresponse()
data = result.read()
finally:
shadir_connection.close()
if result.status != 200: if result.status != 200:
raise DirectoryNotFound(result.read()) raise DirectoryNotFound(result.read())
data = json.loads(result.read())
self.close()
return data
def close(self):
''' It must be called to close the HTTP Connection. '''
if hasattr(self, 'shacache_connection'):
self.shacache_connection.close()
if hasattr(self, 'shadir_connection'): return json.loads(data)
self.shadir_connection.close()
class FileNotFound(Exception): class FileNotFound(Exception):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment