1

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.

Zeth
  • 2,273
  • 4
  • 43
  • 91

1 Answers1

2

I could do this:

test = speedtest.Speedtest(secure=True)

I looked into the source code myself, in this directory:

vim /usr/local/lib/python3.9/site-packages/speedtest.py

Where I would see the function was defined like this:

class Speedtest(object):
    """Class for performing standard speedtest.net testing operations"""

    def __init__(self, config=None, source_address=None, timeout=10,
                 secure=False, shutdown_event=None):
        self.config = {}

        self._source_address = source_address
        self._timeout = timeout
        self._opener = build_opener(source_address, timeout)

        self._secure = secure
...
...
...
Zeth
  • 2,273
  • 4
  • 43
  • 91