'Locust - Python - ssl.SSLError: [SSL] PEM lib (_ssl.c:4065)

I am new to python and locust, trying to run my first locust perf script to test an API However, I am getting error as ssl.SSLError: [SSL] PEM lib (_ssl.c:4065) for following code.

class MyHttpTest(HttpUser):
wait_time = constant(5)
host = "https://abc.myorg.net"
headers = {
    'Content-Type': "application/json",
    'Ocp-Apim-Trace': "true",
    'Ocp-Apim-Subscription-Key': "<APIM subscription Key>",
    'Cache-Control': "no-cache"
}
payload = "{\n\t\"name\": \"abc\"\n}"

@task
def get_status(self):

    self.client.headers = self.headers
    self.client.verify = False

    self.client.post("/uam/Link/Get/v1", data=self.payload,
                     cert=("<path to pfx certificate>",
                           "<password of pfx certificate"))


Solution 1:[1]

Check these out:

How to configure Locust to use https?

What does "SSLError: [SSL] PEM lib (_ssl.c:2532)" mean using the Python ssl library?

Pretty sure the problem is the .pfx file you're trying to use as your cert. Locust's HttpUser's client is based on Requests. I'm currently unable to find anything that says Requests can work with .pfx files directly. You need to have a .cert or .pem format. I haven't tried it, but I found a gist that says it lets you use .pfx with Requests so it ought to work with Locust, I'd imagine.

https://gist.github.com/erikbern/756b1d8df2d1487497d29b90e81f8068

Solution 2:[2]

I overcame this by adding certificate to cacert.pem storage, that is a part of certifi lib which goes with requests library

import certifi
import requests

try:
    print('Checking connection to Github...')
    test = requests.get('https://api.github.com')
    print('Connection to Github OK.')
except requests.exceptions.SSLError as err:
    print('SSL Error. Adding custom certs to Certifi store...')
    cafile = certifi.where()
    with open('certicate.pem', 'rb') as infile:
        customca = infile.read()
    with open(cafile, 'ab') as outfile:
        outfile.write(customca)
    print('That might have worked.')

The exapmple is taken from here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Solowalker
Solution 2