https://github.com/the-last-cookie/pyhole
Python library for the Pi-hole 6.0 REST API
https://github.com/the-last-cookie/pyhole
api api-rest library pi-hole pihole python
Last synced: about 2 months ago
JSON representation
Python library for the Pi-hole 6.0 REST API
- Host: GitHub
- URL: https://github.com/the-last-cookie/pyhole
- Owner: The-Last-Cookie
- License: mit
- Created: 2025-04-21T14:00:17.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-25T23:12:42.000Z (about 1 year ago)
- Last Synced: 2025-04-27T09:59:52.213Z (about 1 year ago)
- Topics: api, api-rest, library, pi-hole, pihole, python
- Language: Python
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# pyhole


Python library for accessing the Pihole 6 REST API.
This project is **not affiliated** with [Pi-hole](https://github.com/pi-hole).
*Note: This is still in active development, so stuff may break!*
## Installation
### Import
Download the `pyhole.py` file and then import it in Python like so:
```py
from pyhole import Pihole
```
### Secure requests
*Notice: This step is required for the script to work!*
It is recommended that you use SSL for your requests to the API. Newer versions of the `request` module require the `KeyUsage` and `ExtendedKeyUsage` parameters to be set. By default, Pi-hole creates a certificate without these parameters. In any case, copy the root CA certificate without the private key (**not** the server certificate!) (by default `/etc/pihole/tls_ca.crt`) to the device where the script is executed (e.g. `/home/user/pi_certificate.pem`). After that, set the `CERT_BUNDLE` variable e.g. in the `config.json` to the path where you stored the certificate.
If you don't want to use SSL, set the `CERT_BUNDLE` variable to `0` (integer). Keep in mind though that the requests made to the API will be unencrypted in this case and warnings will be displayed on the console.
## Example code
```py
from pyhole import Pihole
# get data from config.json
cert = "/path/to/cert.pem or crt"
password = "your_password"
pi = Pihole("https://pi.hole/api", cert)
pi.authenticate(password)
history = pi.metrics.get_history()
devices = pi.network.get_devices()
gateway = pi.network.get_gateway()
```
## References
- Inspiration for the class structure taken from [How to "namespace" methods in Python class](https://stackoverflow.com/questions/48406389/how-to-namespace-methods-in-python-class).
- [Designing Pythonic library APIs](https://benhoyt.com/writings/python-api-design/)