如何使用套接字获取Asterisk服务器的状态-Python [英] How to get the status of an Asterisk Server using a Socket - 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 + '\n'
s.close()
我只收到一条消息,说星号版本,仅此而已.
I just get a message saying Asterisk Version and nothing more.
我希望有人可以帮我这个忙.
I hope somebody could help me with this.
谢谢.
推荐答案
您在此处的代码格式有误. Asterisk AMI要求在命令之间使用\ r \ n终止.
You have malformed your code there. The Asterisk AMI requires \r\n 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 + '\r\n')
data = s.recv(1024)
print data + '\n'
params = 'Action: status'
s.send(params + '\r\n')
data = s.recv(1024)
print data + '\n'
params = 'Action: Logoff'
s.send(params + '\r\n')
data = s.recv(1024)
print data + '\n'
这应该可以解决问题.显然,您还想为其创建一个函数或其他任何函数,但这将使其起作用.
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屋!