netgescon-master/scripts/03_invia_api.py

39 lines
1.3 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Script: 03_invia_api.py
Invia tutti i file JSON nella cartella trasmissioni/ al server REST.
Autore: Pikappa2 2025-05-27
"""
import os, requests, json
BASE = os.path.expanduser('~/netgescon')
TRASM = os.path.join(BASE, 'trasmissioni')
LOG = os.path.join(BASE, 'log/trasmissione.log')
CONFIG = os.path.join(BASE, 'agent_config.json')
with open(CONFIG) as f:
cfg = json.load(f)
ENDPOINT = cfg['RestEndpoint']
HEADERS = {"Authorization": f"Bearer {cfg['AgentKey']}"}
os.makedirs(os.path.dirname(LOG), exist_ok=True)
def main():
with open(LOG, 'a') as log:
log.write(f"==== {__import__('datetime').datetime.now()} - AVVIO TRASMISSIONE ====\n")
for root, dirs, files in os.walk(TRASM):
for file in files:
if file.endswith('.json'):
path = os.path.join(root, file)
try:
with open(path, 'rb') as fjson:
r = requests.post(ENDPOINT, headers=HEADERS, files={'file': (file, fjson)})
log.write(f"{file}: {r.status_code} {r.text[:100]}\n")
except Exception as e:
log.write(f"Errore su {file}: {e}\n")
log.write(f"==== {__import__('datetime').datetime.now()} - FINE TRASMISSIONE ====\n")
if __name__ == "__main__":
main()