How to Perform a Basic Virus Scan in Python

Cloudmersive
2 min readMay 28, 2024

--

Using a free API, we can easily virus-scan files by referencing a database equipped with more than 17 million virus and malware signatures.

We can structure our API call using the ready-to-run Python code examples below, and we can authorize our API calls with a free Cloudmersive API key (this will allow us to make up to 800 API calls per month with no commitments).

It’s important to note that scanning our files with this AV solution won’t identify advanced custom content threats (such as spoofed files and other files designed by threat actors for targeted attacks), but it WILL act as a powerful & scalable anti-malware layer in any of our file upload handling applications. Convenience is a big factor here, too — we’ll abstract the heavy lifting in our malware scanning process to external servers, and all scans are performed in-memory, so none of our data will be written to disc.

To begin structuring our API call, let’s first run the following pip command to install the SDK:

pip install cloudmersive-virus-api-client

After that, we can use the below code examples to add the imports and call the function. We can paste our API key in the ‘YOUR_API_KEY’ placeholder snippet to authorize our requests:

from __future__ import print_function
import time
import cloudmersive_virus_api_client
from cloudmersive_virus_api_client.rest import ApiException
from pprint import pprint

# Configure API key authorization: Apikey
configuration = cloudmersive_virus_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'



# create an instance of the API class
api_instance = cloudmersive_virus_api_client.ScanApi(cloudmersive_virus_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.

try:
# Scan a file for viruses
api_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 that’s all there is to it! Now we have an easy-to-use, flexible, and low-code virus scanning solution for our Python form upload handlers.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet