This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
язык_программирования_python [2025/06/16 14:58] val [FastAPI Uvicorn] |
язык_программирования_python [2025/07/08 09:53] (current) val |
||
|---|---|---|---|
| Line 200: | Line 200: | ||
| * [[https://www.geeksforgeeks.org/python-find-dictionary-matching-value-in-list/|Find dictionary matching value in list - Python]] | * [[https://www.geeksforgeeks.org/python-find-dictionary-matching-value-in-list/|Find dictionary matching value in list - Python]] | ||
| * [[https://docs-python.ru/tutorial/vstroennye-funktsii-interpretatora-python/funktsija-next/|Функция next() в Python, следующий элемент итератора]] | * [[https://docs-python.ru/tutorial/vstroennye-funktsii-interpretatora-python/funktsija-next/|Функция next() в Python, следующий элемент итератора]] | ||
| + | |||
| + | ==== Пример 1 ==== | ||
| <code> | <code> | ||
| gate# apt install python3-xmltodict/stable | gate# apt install python3-xmltodict/stable | ||
| Line 235: | Line 237: | ||
| </code> | </code> | ||
| + | ==== Пример 2 ==== | ||
| + | <code> | ||
| + | # cat /usr/local/bin/asterisk.chansip.discovery.py | ||
| + | </code><code> | ||
| + | #!/usr/bin/env python3 | ||
| + | |||
| + | import subprocess, io, json | ||
| + | |||
| + | result=[] | ||
| + | |||
| + | proc = subprocess.Popen('/usr/sbin/asterisk -x "sip show users" | tail -n +2 | cut -d" " -f1', stdout=subprocess.PIPE, shell=True) | ||
| + | for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): | ||
| + | #print(line.rstrip()) | ||
| + | s={} | ||
| + | s["{#CHANNAME}"]="SIP/"+line.rstrip() | ||
| + | result.insert(1,s) | ||
| + | print(json.dumps(result)) | ||
| + | </code> | ||
| + | |||
| + | Вариант от DeepSeek | ||
| + | |||
| + | <code> | ||
| + | # cat /usr/local/bin/asterisk.chansip.discovery.ds.py | ||
| + | </code><code> | ||
| + | #!/usr/bin/env python3 | ||
| + | |||
| + | import subprocess | ||
| + | import json | ||
| + | |||
| + | # Запускаем команду и сразу получаем вывод | ||
| + | output = subprocess.check_output( | ||
| + | '/usr/sbin/asterisk -x "sip show users" | tail -n +2 | cut -d" " -f1', | ||
| + | shell=True, | ||
| + | text=True | ||
| + | ) | ||
| + | |||
| + | # Формируем список словарей в одну строку | ||
| + | result = [{"{#CHANNAME}": f"SIP/{line.strip()}"} for line in output.splitlines() if line.strip()] | ||
| + | |||
| + | # Выводим результат в формате JSON | ||
| + | print(json.dumps(result)) | ||
| + | </code> | ||
| ===== Zabbix API приложение ===== | ===== Zabbix API приложение ===== | ||