4040 from java .io import BufferedReader , InputStreamReader , OutputStream
4141 from java .net import HttpURLConnection , URL
4242
43- def perform_request (url , method = 'GET' , data = None ):
43+ def perform_request (url , method = 'GET' , data = None , timeout = None ):
4444 try :
4545 connection = URL (url ).openConnection ()
4646 connection .setRequestProperty ("Connection" , "close" )
@@ -73,7 +73,7 @@ def perform_request(url, method='GET', data=None):
7373else :
7474 import urllib2
7575
76- def perform_request (url , method = 'GET' , data = None ):
76+ def perform_request (url , method = 'GET' , data = None , timeout = None ):
7777 """Perform HTTP request with scan server
7878
7979 :param url: URL
@@ -93,7 +93,10 @@ def perform_request(url, method='GET', data=None):
9393 opener = urllib2 .build_opener ()
9494
9595 if method == 'GET' :
96- response = opener .open (req )
96+ if timeout :
97+ response = opener .open (req , timeout = timeout )
98+ else :
99+ response = opener .open (req )
97100
98101 elif method == 'POST' :
99102 response = opener .open (req , data )
@@ -264,43 +267,44 @@ def __submitScanSequence(self, cmdSeq, scanName, queue=True):
264267 return self .__submitScanXML (cmdSeq .genSCN (),scanName , queue )
265268
266269
267- def scanInfos (self ):
270+ def scanInfos (self , timeout = 20 ):
268271 """Get information of all scans
269272
270273 Using `GET {BaseURL}/scans`
271274
275+ :param timeout: Throws exception when no reply within that time in seconds
272276 :return: List of :class:`~scan.client.scaninfo.ScanInfo`
273277
274278 Example::
275279
276280 >>> infos = client.scanInfos()
277281 >>> print [ str(info) for info in infos ]
278282 """
279- xml = perform_request (self .__baseURL + "/scans" )
283+ xml = perform_request (self .__baseURL + "/scans" , timeout = timeout )
280284 scans = ET .fromstring (xml )
281285 result = list ()
282286 for scan in scans .findall ('scan' ):
283287 result .append (ScanInfo (scan ))
284288 return result
285289
286290
287- def scanInfo (self , scanID ):
291+ def scanInfo (self , scanID , timeout = 10 ):
288292 """Get information for a scan
289293
290294 Using `GET {BaseURL}/scan/{id}`
291295
292296 :param scanID: The ID of scan for which to fetch information.
297+ :param timeout: Throws exception when no reply within that time in seconds
293298 :return: :class:`~scan.client.scaninfo.ScanInfo`
294299
295300 Example::
296301
297302 >>> client = ScanClient()
298303 >>> print client.scanInfo(42)
299304 """
300- xml = perform_request (self .__baseURL + "/scan/" + str (scanID ))
305+ xml = perform_request (self .__baseURL + "/scan/" + str (scanID ), timeout = timeout )
301306 return ScanInfo (ET .fromstring (xml ))
302307
303-
304308 def scanCmds (self , scanID ):
305309 """Get the commands of scan.
306310
@@ -381,19 +385,26 @@ def waitUntilDone(self, scanID):
381385
382386 In case the scan failed or was aborted,
383387 an exception is raised.
388+
389+ If scan information is not available
390+ (timeout while requesting it),
391+ keep checking.
384392
385393 :param scanID: ID of scan on which to wait
386394
387395 :return: Scan info
388396 :raise Exception: If scan was aborted or failed.
389397 """
390- info = self .scanInfo (scanID )
391- while not info .isDone ():
398+ while True :
399+ try :
400+ info = self .scanInfo (scanID )
401+ if info .isDone ():
402+ return info
403+ if info .state in ( 'Aborted' , 'Failed' ):
404+ raise Exception (str (info ))
405+ except :
406+ pass
392407 time .sleep (1 )
393- info = self .scanInfo (scanID )
394- if info .state in ( 'Aborted' , 'Failed' ):
395- raise Exception (str (info ))
396- return info
397408
398409
399410 def pause (self , scanID = - 1 ):
0 commit comments