How to virus scan a TAR file in Python

Cloudmersive
2 min readMay 26, 2020

The dangers of un-vetted files looms large over all unprotected projects. If you do not have a virus scanning solution in place, then you came to the right spot. I’m going to show you how to set this up in Python, and do so with very little effort. Let’s dive straight in.

We are going to kick off with our client installation, using pip install, like so:

pip install cloudmersive-virus-api-client

Our next step is a simple API call setup, which we can do with an API instance created with an API key. Here is what that can look like:

from __future__ import print_functionimport timeimport cloudmersive_virus_api_clientfrom cloudmersive_virus_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_virus_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_virus_api_client.ScanApi(cloudmersive_virus_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Scan a file for virusesapi_response = api_instance.scan_file(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ScanApi->scan_file: %s\n" % e)

And with that done we can enter our file path and we are golden! That’s right, there’s really nothing else to it; your virus scanning solution is ready to go.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.