Skip to content

Commit 4098cee

Browse files
author
pjkersha
committed
Extended utils funcs so that they can accept a handlers kw
git-svn-id: http://proj.badc.rl.ac.uk/svn/ndg-security/trunk/ndg_httpsclient@8145 051b1e3e-aa0c-0410-b6c2-bfbade6052be
1 parent 9e44cb7 commit 4098cee

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

ndg/httpsclient/utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,24 @@ class URLFetchError(Exception):
6262
"""Error fetching content from URL"""
6363

6464

65-
def fetch_from_url(url, config, data=None):
65+
def fetch_from_url(url, config, data=None, handlers=None):
6666
"""Returns data retrieved from a URL.
6767
@param url: URL to attempt to open
6868
@type url: basestring
6969
@param config: SSL context configuration
7070
@type config: Configuration
7171
@return data retrieved from URL or None
7272
"""
73-
return_code, return_message, response = open_url(url, config, data)
73+
return_code, return_message, response = open_url(url, config, data=data,
74+
handlers=handlers)
7475
if return_code and return_code == httplib.OK:
7576
return_data = response.read()
7677
response.close()
7778
return return_data
7879
else:
7980
raise URLFetchError(return_message)
8081

81-
def fetch_from_url_to_file(url, config, output_file, data=None):
82+
def fetch_from_url_to_file(url, config, output_file, data=None, handlers=None):
8283
"""Writes data retrieved from a URL to a file.
8384
@param url: URL to attempt to open
8485
@type url: basestring
@@ -91,7 +92,8 @@ def fetch_from_url_to_file(url, config, output_file, data=None):
9192
returned message
9293
boolean indicating whether access was successful)
9394
"""
94-
return_code, return_message, response = open_url(url, config, data)
95+
return_code, return_message, response = open_url(url, config, data=data,
96+
handlers=handlers)
9597
if return_code == httplib.OK:
9698
return_data = response.read()
9799
response.close()
@@ -100,7 +102,7 @@ def fetch_from_url_to_file(url, config, output_file, data=None):
100102
outfile.close()
101103
return return_code, return_message, return_code == httplib.OK
102104

103-
def fetch_stream_from_url(url, config, data=None):
105+
def fetch_stream_from_url(url, config, data=None, handlers=None):
104106
"""Returns data retrieved from a URL.
105107
@param url: URL to attempt to open
106108
@type url: basestring
@@ -109,14 +111,15 @@ def fetch_stream_from_url(url, config, data=None):
109111
@return: data retrieved from URL or None
110112
@rtype: file derived type
111113
"""
112-
return_code, return_message, response = open_url(url, config, data)
114+
return_code, return_message, response = open_url(url, config, data=data,
115+
handlers=handlers)
113116
if return_code and return_code == httplib.OK:
114117
return response
115118
else:
116119
raise URLFetchError(return_message)
117120

118121

119-
def open_url(url, config, data=None):
122+
def open_url(url, config, data=None, handlers=None):
120123
"""Attempts to open a connection to a specified URL.
121124
@param url: URL to attempt to open
122125
@param config: SSL context configuration
@@ -141,7 +144,10 @@ def open_url(url, config, data=None):
141144
# urllib2.HTTPCookieProcessor which replaces cookies).
142145
cookie_handler = AccumulatingHTTPCookieProcessor(cj)
143146

144-
handlers = [cookie_handler]
147+
if not handlers:
148+
handlers = []
149+
150+
handlers.append(cookie_handler)
145151

146152
if config.debug:
147153
http_handler = HTTPHandler(debuglevel=debuglevel)

0 commit comments

Comments
 (0)