'''Files=====Methods described in this section relate to the files API.These methods can be accessed at ``Nessus.files``... rst-class:: hide-signature.. autoclass:: FilesAPI :members:'''fromtenable.base.endpointimportAPIEndpointfromtypingimportOptional,Callable,DictfromrequestsimportResponsefromioimportBytesIO
[docs]defupload(self,fobj:BytesIO,encrypted:bool=False)->str:''' Uploads a file to Tenable Nessus Args: fobj (BytesIO): The file object to upload encrypted (bool, optional): Is the file encrypted? Returns: str: File identifier to be used in future calls. Example: >>> with open('example.txt', 'rb') as fobj: ... fn = nessus.files.upload(fobj) '''returnself._post('upload',data={'no_enc':int(encrypted)},files={'Filedata':fobj})['fileuploaded']
def_download(self,response:Response,fobj:Optional[BytesIO]=None,chunk_size:int=1024,stream_hook:Optional[Callable[[Response,BytesIO,int],BytesIO]]=None,hook_kwargs:Optional[Dict]=None)->BytesIO:''' File download manager for Tenable Nessus Args: response (Response): The Response object to work on fobj (BytesIO, optional): The file object to write to. If unspecified, an in-memory object will be created and returned chunk_size (int, optional): The chunk size to use when writing to the file object. The default is unspecified is ``1024`` bytes. stream_hook (Callable[Response, BytesIO, int], optional): If specified, the output will be passed to the stream hook instead of processing ourselves. hook_kwargs (Dict, optional): Any additional keyword arguments that should be passed on to the stream hook. Returns: BytesIO: The file object '''defbase_hook(resp:Response,fobj:BytesIO,chunk_size:int,**kwargs):''' Default stream hook '''forchunkinresp.iter_content(chunk_size=chunk_size):ifchunk:fobj.write(chunk)# Set the default attributes values if nothing was passed to themiffobjisNone:fobj=BytesIO()ifhook_kwargsisNone:hook_kwargs={}ifstream_hookisNone:stream_hook=base_hook# Call the stream hook with the Response object passed to usstream_hook(response,fobj,chunk_size,**hook_kwargs)# seek the file back to the beginning, close the response, and return# the file object to the callerfobj.seek(0)response.close()returnfobj