Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

Implement get internet speed demo function in Python #309

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions python/get-internet-speed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## 📡 Function for getting internet speed
A sample Python Cloud Function to check the download and upload speed (and related statistics) of the server.

## 📝 Environment Variables
No environment variables needed.

🚀 Building and Packaging
To package this example as a cloud function, follow these steps.

```bash
$ cd demos-for-functions/python/get-internet-speed

$ PIP_TARGET=./.appwrite pip install -r ./requirements.txt --upgrade --ignore-installed
```

* Ensure that your folder structure looks like this
```
.
├── .appwrite/
├── main.py
├── README.md
└── requirements.txt
```

* Create a tarfile

```bash
$ cd ..
$ tar -zcvf code.tar.gz get-internet-speed
```

* Upload the tarfile to your Appwrite Console and use the following entrypoint command

```bash
python main.py
```

* Click 'Activate'
* Increase the timeout for the function to at least 120 sec or more (in Function setting > Timeout field).
* Depending on the location and the server's speed, you will need to adjust the timeout accordingly.

## 🎯 Trigger
After the Building and Packaging step, follow the following steps:

1. Press the Execute Now button.
2. Wait for the function to finish
3. Check the Logs for the result
Example output: `{'download_speed': '45.07 Mbps', 'upload_speed': '61.02 Mbps', 'ping': '7.274 ms'}`

15 changes: 15 additions & 0 deletions python/get-internet-speed/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from speedtest import Speedtest

s = Speedtest()
s.download()
s.upload()

results_dict = s.results.dict()

pretty_results_dict = {
"download_speed": f"{(results_dict['download'] / 1000.0 / 1000.0):.2f} Mbps",
"upload_speed": f"{(results_dict['upload'] / 1000.0 / 1000.0):.2f} Mbps",
"ping": f"{results_dict['ping']} ms",
}

print(pretty_results_dict)
1 change: 1 addition & 0 deletions python/get-internet-speed/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
speedtest-cli