为什么我的 asterisk 服务器没有收到带有以下扭曲 SIP python 代码的响应

Why am I not getting a response from my asterisk server with the following twisted SIP python code

使用此处找到的代码 https://github.com/twisted/twisted/blob/trunk/src/twisted/test/test_sip.py

我已经尝试向我的本地 (127.0.0.1) 星号服务器发送简单的 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:joe@127.0.1.1>")
        r.addHeader("From", "<sip:joe@127.0.1.1>")
        r.addHeader("Call-ID", "<1opt@127.0.1.1>")
        r.addHeader("CSeq", "1 OPTIONS")
        r.addHeader("contact", "<sip:joe@127.0.1.1: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

在星号上,我可以看到消息到达。
但是星号从不发送响应 200 ok,或错误或任何其他对我理解我的代码有什么问题有用的东西。

*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:joe@127.0.1.1>
From: <sip:joe@127.0.1.1>
Call-ID: <1opt@127.0.1.1>
CSeq: 1 OPTIONS
Contact: <sip:joe@127.0.1.1: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:service@127.0.0.1>
From: <sip:service@127.0.0.1>;tag=1
Call-ID: 1-19903@127.0.1.1
CSeq: 1 OPTIONS
Contact: <sip:service@127.0.1.1: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:service@127.0.0.1>;tag=1
To: <sip:service@127.0.0.1>;tag=as6e9328e8
Call-ID: 1-19903@127.0.1.1
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 '1-19903@127.0.1.1' in 32000 ms (Method: OPTIONS)
Really destroying SIP dialog '1-19903@127.0.1.1' Method: OPTIONS

我看到的是 SIP 正在后台做一些其他工作来初始化 SIP 对话。 我认为这是上面的示例代码中缺少的。 当 asterisk 正确启动传输到客户端 sipp 正在创建的响应时。

删除 r.addHeader("CSeq", "1 OPTIONS").
我现在正在让我的本地星号响应

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:joe@127.0.1.1>")
    r.addHeader("From", "<sip:joe@127.0.1.1>")
    r.addHeader("Call-ID", "<1opt@127.0.1.1>")
    # r.addHeader("CSeq", "1 OPTIONS")
    r.addHeader("contact", "<sip:joe@127.0.1.1: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

所以我想我需要建立一种方法来正确处理调用序列或忽略它。

有星号张贴的警告。
chan_sip.c:11681 copy_header: No field 'CSeq' present to copy
但至少我的测试现在有效

    <--- SIP read from UDP:127.0.0.1:46947 --->
    OPTIONS sip:127.0.0.1 SIP/2.0
    Via: SIP/2.0/UDP 127.0.0.1:5062;rport=5062
    Max-Forward: 3
    To: <sip:OptionsCheckMonitor@127.0.0.1>
    From: <sip:OptionsCheckMonitor@127.0.0.1>
    Call-ID: <1optioncheck@127.0.0.1>
    Contact: <sip:OptionsCheckMonitor@127.0.0.1;transport=UDP>
    Accept: application/sdp
    Content-Length: 0

    <------------->
    --- (9 headers 0 lines) ---
    Sending to 127.0.0.1:5062 (no NAT)
    [Jan 24 12:52:54] NOTICE[12752]: chan_sip.c:11681 copy_header: No field 'CSeq' present to copy

    <--- Transmitting (no NAT) to 127.0.0.1:5062 --->
    SIP/2.0 400 Bad Request
    Via: SIP/2.0/UDP 127.0.0.1:5062;rport=5062;received=127.0.0.1
    From: <sip:OptionsCheckMonitor@127.0.0.1>
    To: <sip:OptionsCheckMonitor@127.0.0.1>;tag=as7d34b365
    Call-ID: <1optioncheck@127.0.0.1>
    Server: Asterisk PBX 13.10.0~dfsg-1ubuntu2
    Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
    Supported: replaces, timer
    Content-Length: 0