Last active
June 8, 2018 21:24
-
-
Save rsyring/7a05cb2fed60bc92d4ecf6ab75c4aedd to your computer and use it in GitHub Desktop.
Streaming salt-api events using python requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import arrow | |
session = requests.Session() | |
resp = session.post( | |
'http://172.18.0.3:8765/login', | |
json={ | |
'username': 'tang-api', | |
'password': 'notsecure', | |
'eauth': 'pam', | |
}, | |
) | |
data = resp.json()['return'][0] | |
token = data['token'] | |
expire = arrow.get(data['expire']) | |
print(f'Token {token} expires: {expire.humanize()}') | |
resp = session.get('http://172.18.0.3:8765/', json=[{ | |
'client': 'local', | |
'tgt': '*', | |
'fun': 'test.ping' | |
}]) | |
print(resp.json()) | |
with session.post('http://172.18.0.3:8765/events', stream=True) as r: | |
for line in r.iter_lines(decode_unicode=True): | |
print(line) | |
if r.status_code != 200: | |
print(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment