Call Speedtest. Speedtest() from Python Using --Secure (To Avoid Speedtest. Configretrievalerror: Http Error 403: Forbidden)

I have a small Python3-script like this:

import speedtest

# Speedtest
test = speedtest.Speedtest()         # <--- line 4

test.get_servers()
best = test.get_best_server()
print(f"Found: {best['host']} located in {best['country']}")

The first time I run it, it works and everything is fine; it outputs:

Found: speedtest.witcom.cloud:8080 located in Germany

Happy days.

The second time (and subsequel times) that I run the script, I get this error:

Traceback (most recent call last):
  File "/Users/zeth/Code/pinger/pinger.py", line 4, in <module>
    test = speedtest.Speedtest()
  File "/usr/local/lib/python3.9/site-packages/speedtest.py", line 1095, in __init__
    self.get_config()
  File "/usr/local/lib/python3.9/site-packages/speedtest.py", line 1127, in get_config
    raise ConfigRetrievalError(e)
speedtest.ConfigRetrievalError: HTTP Error 403: Forbidden

When Googling around, I saw that I could also call this module straight from the command line, but just running this:

$ speedtest-cli

That gives me the same kind of error:

Retrieving speedtest.net configuration...
Cannot retrieve speedtest configuration
ERROR: HTTP Error 403: Forbidden

But if I run the direct cli-command: speedtest-cli --secure ( docs for the --secure-flag ), then it goes through and outputs this:

Retrieving speedtest.net configuration...
Testing from Deutsche Telekom AG (212.185.228.168)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by hotspot.koeln (Cologne) [3.44 km]: 28.805 ms
Testing download speed................................................................................
Download: 30.01 Mbit/s
Testing upload speed......................................................................................................
Upload: 8.68 Mbit/s

The question

I can't figure out how to change this Python-line: test = speedtest.Speedtest() to use a --secure-flag (nor via HTTPS).

The documentation for speedtest-cli is scarce.

Other attempts

I found this solution here: Python Speedtest facing problems with certification _ssl.c:1056, that suggests manually approving the certificates.

But in this directory: /Volumes/Macintosh HD/Applications/ I don't have anything called Python3.9. I have python3.9 installed via Brew. And I'm on a Mac.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.