@@ -39,14 +39,15 @@ class NetstorageError(Exception):
3939
4040
4141class Netstorage :
42- def __init__ (self , hostname , keyname , key , ssl = False ):
42+ def __init__ (self , hostname , keyname , key , ssl = False , timeout = None ):
4343 if not (hostname and keyname and key ):
4444 raise NetstorageError ('[NetstorageError] You should input netstorage hostname, keyname and key all' )
4545
4646 self .hostname = hostname
4747 self .keyname = keyname
4848 self .key = key
4949 self .ssl = 's' if ssl else ''
50+ self .timeout = timeout
5051 self .http_client = requests .Session ()
5152
5253
@@ -105,102 +106,117 @@ def _request(self, **kwargs):
105106 'Accept-Encoding' : 'identity' ,
106107 'User-Agent' : 'NetStorageKit-Python'
107108 }
109+
110+ timeout = kwargs ['timeout' ] if 'timeout' in kwargs else self .timeout
108111
109112 response = None
110113 if kwargs ['method' ] == 'GET' :
111114 if kwargs ['action' ] == 'download' :
112- response = self .http_client .get (request_url , headers = headers , stream = True )
115+ response = self .http_client .get (request_url , headers = headers , timeout = timeout , stream = True )
113116 if 'stream' not in kwargs .keys ():
114117 self ._download_data_from_response (response , kwargs ['path' ], kwargs ['destination' ])
115118 else :
116- response = self .http_client .get (request_url , headers = headers )
119+ response = self .http_client .get (request_url , headers = headers , timeout = timeout )
117120
118121 elif kwargs ['method' ] == 'POST' :
119- response = self .http_client .post (request_url , headers = headers )
122+ response = self .http_client .post (request_url , headers = headers , timeout = timeout )
120123
121124 elif kwargs ['method' ] == 'PUT' : # Use only upload
122125 if 'stream' in kwargs .keys ():
123- response = self .http_client .put (request_url , headers = headers , data = kwargs ['stream' ])
126+ response = self .http_client .put (request_url , headers = headers , timeout = timeout , data = kwargs ['stream' ])
124127 elif kwargs ['action' ].startswith ('upload' ):
125128 mmapped_data = self ._upload_data_to_request (kwargs ['source' ])
126- response = self .http_client .put (request_url , headers = headers , data = mmapped_data )
129+ response = self .http_client .put (request_url , headers = headers , timeout = timeout , data = mmapped_data )
127130 if not isinstance (mmapped_data , str ):
128131 mmapped_data .close ()
129132
130133 return response .status_code == 200 , response
131134
132- def dir (self , ns_path , option = {}):
135+ def dir (self , ns_path , option = {}, timeout = None ):
133136 option = "dir&format=xml&{0}" .format (urlencode (option ))
134137 return self ._request (action = option ,
135138 method = 'GET' ,
136- path = ns_path )
139+ path = ns_path ,
140+ timeout = timeout )
137141
138- def list (self , ns_path , option = {}):
142+ def list (self , ns_path , option = {}, timeout = None ):
139143 option = "list&format=xml&{0}" .format (urlencode (option ))
140144 return self ._request (action = option ,
141145 method = 'GET' ,
142- path = ns_path )
146+ path = ns_path ,
147+ timeout = timeout )
143148
144- def download (self , ns_source , local_destination = '' ):
149+ def download (self , ns_source , local_destination = '' , timeout = None ):
145150 if ns_source .endswith ('/' ):
146151 raise NetstorageError ("[NetstorageError] Nestorage download path shouldn't be a directory: {0}" .format (ns_source ))
147152 return self ._request (action = 'download' ,
148153 method = 'GET' ,
149154 path = ns_source ,
150- destination = local_destination )
155+ destination = local_destination ,
156+ timeout = timeout )
151157
152- def stream_download (self , ns_source ):
158+ def stream_download (self , ns_source , timeout = None ):
153159 return self ._request (action = 'download' ,
154160 method = 'GET' ,
155161 path = ns_source ,
156- stream = True )
162+ stream = True ,
163+ timeout = timeout )
157164
158- def du (self , ns_path ):
165+ def du (self , ns_path , timeout = None ):
159166 return self ._request (action = 'du&format=xml' ,
160167 method = 'GET' ,
161- path = ns_path )
168+ path = ns_path ,
169+ timeout = timeout )
162170
163- def stat (self , ns_path ):
171+ def stat (self , ns_path , timeout = None ):
164172 return self ._request (action = 'stat&format=xml' ,
165173 method = 'GET' ,
166- path = ns_path )
174+ path = ns_path ,
175+ timeout = timeout )
167176
168- def mkdir (self , ns_path ):
177+ def mkdir (self , ns_path , timeout = None ):
169178 return self ._request (action = 'mkdir' ,
170179 method = 'POST' ,
171- path = ns_path )
180+ path = ns_path ,
181+ timeout = timeout )
172182
173- def rmdir (self , ns_path ):
183+ def rmdir (self , ns_path , timeout = None ):
174184 return self ._request (action = 'rmdir' ,
175185 method = 'POST' ,
176- path = ns_path )
186+ path = ns_path ,
187+ timeout = timeout )
177188
178- def mtime (self , ns_path , mtime ):
189+ def mtime (self , ns_path , mtime , timeout = None ):
179190 return self ._request (action = 'mtime&format=xml&mtime={0}' .format (mtime ),
180191 method = 'POST' ,
181- path = ns_path )
192+ path = ns_path ,
193+ timeout = timeout )
182194
183- def delete (self , ns_path ):
195+ def delete (self , ns_path , timeout = None ):
184196 return self ._request (action = 'delete' ,
185197 method = 'POST' ,
186- path = ns_path )
198+ path = ns_path ,
199+ timeout = timeout )
187200
188- def quick_delete (self , ns_path ):
201+ def quick_delete (self , ns_path , timeout = None ):
189202 return self ._request (action = 'quick-delete&quick-delete=imreallyreallysure' ,
190203 method = 'POST' ,
191- path = ns_path )
204+ path = ns_path ,
205+ timeout = timeout )
192206
193- def rename (self , ns_target , ns_destination ):
207+ def rename (self , ns_target , ns_destination , timeout = None ):
194208 return self ._request (action = 'rename&destination={0}' .format (quote_plus (ns_destination )),
195209 method = 'POST' ,
196- path = ns_target )
210+ path = ns_target ,
211+ timeout = timeout )
197212
198- def symlink (self , ns_target , ns_destination ):
213+ def symlink (self , ns_target , ns_destination , timeout = None ):
199214 return self ._request (action = 'symlink&target={0}' .format (quote_plus (ns_target )),
200215 method = 'POST' ,
201- path = ns_destination )
216+ path = ns_destination ,
217+ timeout = timeout )
202218
203- def upload (self , local_source , ns_destination , index_zip = False ):
219+ def upload (self , local_source , ns_destination , index_zip = False , timeout = None ):
204220 if os .path .isfile (local_source ):
205221 if ns_destination .endswith ('/' ):
206222 ns_destination = "{0}{1}" .format (ns_destination , ntpath .basename (local_source ))
@@ -214,10 +230,12 @@ def upload(self, local_source, ns_destination, index_zip=False):
214230 return self ._request (action = action ,
215231 method = 'PUT' ,
216232 source = local_source ,
217- path = ns_destination )
233+ path = ns_destination ,
234+ timeout = timeout )
218235
219- def stream_upload (self , data , ns_destination ):
236+ def stream_upload (self , data , ns_destination , timeout = None ):
220237 return self ._request (action = 'upload' ,
221238 method = 'PUT' ,
222239 stream = data ,
223- path = ns_destination )
240+ path = ns_destination ,
241+ timeout = timeout )
0 commit comments