如何使用套接字获取 Asterisk 服务器的状态 - Python [英] How to get the status of an Asterisk Server using a Socket - Python

查看:28
本文介绍了如何使用套接字获取 Asterisk 服务器的状态 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 套接字获取 Asterisk 服务器的状态,但没有任何反应.

I'm trying to get the status of an Asterisk Server using a python socket but nothing happens.

这是我的代码:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '192.168.1.105'
PORT = 5038

s.connect((HOST, PORT))

params = """Action: login
Events: off
Username: admin
Secret: mypass

Action: status
Action: Logoff
"""

s.send(params)
data = s.recv(1024)
print data + '
'
s.close()

我只收到一条消息,上面写着 Asterisk 版本,仅此而已.

I just get a message saying Asterisk Version and nothing more.

我希望有人能帮我解决这个问题.

I hope somebody could help me with this.

提前致谢.

推荐答案

您的代码格式不正确.Asterisk AMI 需要在命令之间 终止.

You have malformed your code there. The Asterisk AMI requires termination between commands.

您需要在单独的数据包中发送每个命令:

You need to send each command in a separate packet:

params = """Action: login
Events: off
Username: admin
Secret: mypass"""

s.send(params + '
')
data = s.recv(1024)
print data + '
'

params = 'Action: status'
s.send(params + '
')
data = s.recv(1024)
print data + '
'

params = 'Action: Logoff'
s.send(params + '
')
data = s.recv(1024)
print data + '
'

这应该可以解决问题.显然,您还想为它创建一个函数或其他任何东西,但这将使它起作用.

That should do the trick. Obviously you'll want to also make a function for it or whatever, but that will make it work.

始终将 AMI 命令分开!

Always separate AMI commands out!

这篇关于如何使用套接字获取 Asterisk 服务器的状态 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆