import requests import time import json import sys def initiateScan(url, authToken, path, avList = "all"): response = {'success': False, 'message': '', 'data': None} files = {'path': open(path, 'rb')} #We will be using all scanners in this example data = {'avList': avList} #Execute result = requests.post(url, files=files, data=data, headers={'X-Auth-Token': authToken}) result = json.loads(result.text) if result["success"] == False: response["message"] = result["message"] return response response["success"] = True response["data"] = result["data"]["scan_token"] return response def getScanResults(url, authToken): response = {'success': False, 'message': '', 'data': None} result = requests.get(url, headers={'X-Auth-Token': authToken}) result = json.loads(result.text) if result["success"] == False: response["message"] = result["message"] return response response["success"] = True response["data"] = result["data"] return response ##Retrieve temporary token #Provide an actual path with your desired file here path = "YOUR_FILE_PATH_HERE" #Provide your actual authentication token here, if you still don't have one you #can get one at https://kleenscan.com/profile by clicking on 'Generate' button authToken = "YOUR_AUTH_TOKEN_HERE" #Scan URL scanRequestUrl = "https://kleenscan.com/api/v1/file/scan" #In order to make a scan, we need to send a POST containing our file, as well as #the list of scanners we would like to use print("Initiating scan for path: " + path) result = initiateScan(scanRequestUrl, authToken, path) if result["success"] == False: print(result["message"]) sys.exit() routeToken = result["data"] print("Scan token was retrieved successfully, getting scan results..") ##After retrieving the scan token, we are ready to check our scan results #Result URL scanResultsUrl = "https://kleenscan.com/api/v1/file/result/" + routeToken; ##Scanning does not last for more than 5 minutes, we are about to do several # successive iterations in order to retrieve our scan results maxIterations = 6 currentIteration = 0 retrieveResults = True #Retrieve results while retrieveResults: result = getScanResults(scanResultsUrl, authToken) if result["success"] == False: print(result["message"]) sys.exit() scanners = result["data"] scannedCount = 0 for scanner in scanners: if scanner["status"] == "ok": scannedCount += 1 #In case all scanners are done if scannedCount == len(scanners): print("All scanners have finished:") print(scanners) sys.exit() #Wait for next round else: print("Some scanners are still working (" + str(scannedCount) + "/" + str(len(scanners)) + ") done, sleeping for 1 minute ..") currentIteration += 1 if currentIteration == maxIterations: retrieveResults = False print("All scanners have finished:") print(scanners) sys.exit() time.sleep(60)