使用代碼的響應這裏https://github.com/twisted/twisted/blob/trunk/src/twisted/test/test_sip.py爲什麼我沒有得到從我的Asterisk服務器與下述加捻SIP Python代碼
發現我試圖把我的本地(127.0.0.1)的Asterisk服務器簡單的SIP選項檢查。
From twisted.protocols import sip
From twisted.internet import defer, reactor
class Client(sip.Base):
def __init__(self):
sip.Base.__init__(self)
self.received = []
self.deferred = defer.Deferred()
def handle_response(self, response, addr):
self.received.append(response)
self.deferred.callback(self.received)
class OptionsC():
def setup(self):
self.client = Client()
self.clientPort = reactor.listenUDP(
5062, self.client, interface="127.0.1.1")
def testRegisterOPT(self):
p = self.clientPort.getHost().port
r = sip.Request("OPTIONS", "sip:127.0.0.1")
r.addHeader("via", sip.Via("127.0.1.1", port=5062, rport=5062, branch="test123").toString())
r.addHeader("to", "<sip:[email protected]>")
r.addHeader("From", "<sip:[email protected]>")
r.addHeader("Call-ID", "<[email protected]>")
r.addHeader("CSeq", "1 OPTIONS")
r.addHeader("contact", "<sip:[email protected]:5062;transport=UDP>")
r.addHeader("Accept", "application/sdp")
r.addHeader("Content-Length", "0")
print(r.toString())
self.client.sendMessage(
sip.URL(host="127.0.0.1", port=5060), r)
d = self.client.deferred
def check(received):
self.assertEqual(len(received), 1)
r = received[0]
print(r)
print(r.code)
print(dir(r))
self.assertEqual(r.code, 200)
d.addCallback(check)
return d
opc = OptionsC()
opc.setup()
res = opc.testRegisterOPT()
print("test")
這是我的環境中看起來像
$ pip freeze
constantly==15.1.0
incremental==16.10.1
Twisted==16.6.0
zope.interface==4.3.3
$ python -V
Python 2.7.5
Asterisk的,我可以看到郵件到達。
但是,星號永遠不會發送響應200確定,或者錯誤或其他任何有用的信息,以便我瞭解我的代碼出了什麼問題。
*CLI> sip set debug on
SIP Debugging enabled
<--- SIP read from UDP:127.0.1.1:5062 --->
OPTIONS sip:127.0.0.1 SIP/2.0
Via: SIP/2.0/UDP 127.0.1.1:5062;branch=test123;rport=5062
To: <sip:[email protected]>
From: <sip:[email protected]>
Call-ID: <[email protected]>
CSeq: 1 OPTIONS
Contact: <sip:[email protected]:5062;transport=UDP>
Accept: application/sdp
Content-Length: 0
<------------->
--- (9 headers 0 lines) ---
比較這與以下sipp命令,做一些事情非常相似。
$ sipp -sf ./options.xml -m 1 -max_non_invite_retrans 1 127.0.0.1:5060
Resolving remote host '127.0.0.1'... Done.
$ cat ./options.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE scenario SYSTEM "sipp.dtd">
<scenario name="Options">
<send retrans="500" start_rtd="opt-timer">
<![CDATA[
OPTIONS sip:[remote_ip] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
Max-Forwards: 70
To: <sip:[service]@[remote_ip]>
From: <sip:[service]@[remote_ip]>;tag=[call_number]
Call-ID: [call_id]
CSeq: [cseq] OPTIONS
Contact: <sip:[service]@[local_ip]:[local_port];transport=[transport]>
Accept: application/sdp
Content-Length: 0
]]>
</send>
<recv response="200" rrs="true" rtd="opt-timer"></recv>
<!-- definition of the response time repartition table (unit is ms) -->
<ResponseTimeRepartition value="10, 20, 30, 40, 50, 100, 150, 200"/>
<!-- definition of the call length repartition table (unit is ms) -->
<CallLengthRepartition value="10, 50, 100, 500, 1000, 5000, 10000"/>
</scenario>
其中星號正確產生於
<--- SIP read from UDP:127.0.0.1:5061 --->
OPTIONS sip:127.0.0.1 SIP/2.0
Via: SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-19903-1-0
Max-Forwards: 70
To: <sip:[email protected]>
From: <sip:[email protected]>;tag=1
Call-ID: [email protected]
CSeq: 1 OPTIONS
Contact: <sip:[email protected]:5061;transport=UDP>
Accept: application/sdp
Content-Length: 0
<------------->
--- (10 headers 0 lines) ---
Sending to 127.0.0.1:5061 (NAT)
Looking for s in public (domain 127.0.0.1)
<--- Transmitting (NAT) to 127.0.0.1:5061 --->
SIP/2.0 200 OK
Via: SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-19903-1-0;received=127.0.0.1;rport=5061
From: <sip:[email protected]>;tag=1
To: <sip:[email protected]>;tag=as6e9328e8
Call-ID: [email protected]
CSeq: 1 OPTIONS
Server: Asterisk PBX 13.10.0~dfsg-1ubuntu2
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Contact: <sip:127.0.0.1:5060>
Accept: application/sdp
Content-Length: 0
<------------>
Scheduling destruction of SIP dialog '[email protected]' in 32000 ms (Method: OPTIONS)
Really destroying SIP dialog '[email protected]' Method: OPTIONS
響應什麼,我可以看到的是,SIP是做後臺的一些其他工作來初始化一個SIP對話。 我認爲這是上面這個示例代碼中缺少的。 由於星號正確啓動傳輸到客戶端的響應sipp正在創建。