如何使用Python套接字发送SIP消息
问题描述:
我需要使用Python套接字发送SIP消息,我已经让客户端向服务器发送了一些消息,但是我无法让该客户端向服务器发送SIP消息INVITE如何使用Python套接字发送SIP消息
#!/usr/bin/python
import socket
R_IP = '192.168.2.1'
R_PORT = 5060
message = 'INVITE sip:[email protected] SIP/2.0 To: <sip:[email protected]>\x0d\x0aFrom: sip:[email protected];tag=R400_BAD_REQUEST;taag=4488.1908442942.0\x0d\x0aP-Served-User: sip:[email protected]\x0d\x0aCall-ID: [email protected]\x0d\x0aCSeq: 1 INVITE\x0d\x0aContact: sip:[email protected]\x0d\x0aMax-Forwards: 70\x0d\x0aVia: SIP/2.0/TCP 10.44.40.47;branch=z9hG4bK1908442942.4488.0\x0d\x0aContent-Length: 10\x0d\x0a\x0d\x0aRandomText'
def sendPacket():
proto = socket.getprotobyname('tcp')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#, proto)
try:
s.connect((R_IP , R_PORT))
s.sendall(message)
except socket.error:
pass
finally:
s.close()
sendPacket()
你有什么想法吗?
答
好吧,我知道,meybe它会帮助别人。
正确的格式SIP消息如下:
INVITE sip:[email protected] SIP/2.0
To: <to>
Call-ID: <call_id>
<empty line>
body
所以,
message = 'INVITE sip:[email protected] SIP/2.0\r\nTo: <sip:[email protected]>\r\nFroma: sip:[email protected];tag=R400_BAD_REQUEST;taag=4488.1908442942.0\r\nP-Served-User: sip:[email protected]\r\nCall-ID: [email protected]\r\nCSeq: 1 INVITE\r\nContact: sip:[email protected]\r\nMax-Forwards: 70\r\nVia: SIP/2.0/TCP 10.44.40.47;branch=z9hG4bK1908442942.4488.0\r\nContent-Length: 10\r\n\r\nRandomText'
\r\n
是非常重要的,而不空的空间