Salt Modul modules/oss printer.py

Aus CEPHALIX/CRANIX
Version vom 1. Mai 2020, 18:11 Uhr von Admin (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<pre> import salt import subprocess import json import requests # Get default printer def get_default_printer(): #session = subprocess.check_output("c…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
import salt
import subprocess
import json
import requests

# Get default printer
def get_default_printer():
        #session = subprocess.check_output("curl --silent --insecure -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d \"username=ossreader&password=ossreader\" 'https://admin/api/sessions'")
        headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }
        data = {'username':'ossreader', 'password':'ossreader'}
        response = requests.post('https://admin.' + __grains__['domain'] + '/api/sessions', headers=headers, params=data)
        try:
                response.raise_for_status()
                token = response.json()["token"]
        except:
                return ''
        try:
                #defprinter = subprocess.check_output("curl --silent --insecure -X GET --header 'Accept: text/plain' --header \"Authorization: Bearer " + token + "\" 'https://admin/api/sessions/defaultPrinter'")
                headers = {'Authorization': 'Bearer ' + token, 'Accept': 'text/plain' }
                response = requests.get('https://admin.' + __grains__['domain'] + '/api/sessions/defaultPrinter', headers=headers)
                reponse.raise_for_status()
        except:
                return response.text
        finally:
                #subprocess.popen("curl --silent --insecure -X DELETE --header 'Content-Type: application/json' --header 'Accept: application/json' --header \"Authorization: Bearer " + token + "\" \"https://admin/api/sessions/" + token + "\"")
                headers['Accept'] = 'application/json'
                response = requests.delete('https://admin.' + __grains__['domain'] + '/api/sessions/' + token, headers=headers)