Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Python client for the Ubiquiti UDM Pro
import requests
import json
import urllib3
UNIFI_LOGIN_PATH = '/api/auth/login'
# Disable SSL verification warnings
urllib3.disable_warnings()
class Unifi(object):
def __init__(self, host, username, password):
self.host = host
self.username = username
self.password = password
self.session = requests.Session()
self.csrf = ""
def login(self):
payload = {
"username": self.username,
"password": self.password,
}
r = self.request(UNIFI_LOGIN_PATH, payload)
return r.ok
def request(self, path, data={}, method='POST'):
uri = 'https://{}{}'.format(self.host, path)
headers = {
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8",
}
if self.csrf:
headers["X-CSRF-Token"] = self.csrf
r = getattr(self.session, method.lower())(uri, json=data, verify=False, headers=headers)
try:
self.csrf = r.headers['X-CSRF-Token']
except KeyError:
pass
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment