title: Why am I not getting a response from my asterisk server with the following twisted SIP python code
body: <p>Using the code found here <a href="https://github.com/twisted/twisted/blob/trunk/src/twisted/test/test_sip.py" rel="nofollow noreferrer">https://github.com/twisted/twisted/blob/trunk/src/twisted/test/test_sip.py</a></p>
  
  <p>I have tried to send my local(127.0.0.1) asterisk server simple SIP options check.  </p>
  
  <pre><code>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", "&lt;sip:joe@127.0.1.1&gt;")
          r.addHeader("From", "&lt;sip:joe@127.0.1.1&gt;")
          r.addHeader("Call-ID", "&lt;1opt@127.0.1.1&gt;")
          r.addHeader("CSeq", "1 OPTIONS")
          r.addHeader("contact", "&lt;sip:joe@127.0.1.1:5062;transport=UDP&gt;")
          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")
  </code></pre>
  
  <p>This is what my environment looks like  </p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>On asterisk, I can see that the message arrives.<br>
  But asterisk never sends a response 200 ok, or error or anything else useful for me to understand what is wrong with my code. </p>
  
  <pre><code>*CLI&gt; sip set debug on
  SIP Debugging enabled
  
  &lt;--- SIP read from UDP:127.0.1.1:5062 ---&gt;
  OPTIONS sip:127.0.0.1 SIP/2.0
  Via: SIP/2.0/UDP 127.0.1.1:5062;branch=test123;rport=5062
  To: &lt;sip:joe@127.0.1.1&gt;
  From: &lt;sip:joe@127.0.1.1&gt;
  Call-ID: &lt;1opt@127.0.1.1&gt;
  CSeq: 1 OPTIONS
  Contact: &lt;sip:joe@127.0.1.1:5062;transport=UDP&gt;
  Accept: application/sdp
  Content-Length: 0
  
  &lt;-------------&gt;
  --- (9 headers 0 lines) ---
  </code></pre>
  
  <p>Compare this with the following <a href="http://sipp.sourceforge.net/" rel="nofollow noreferrer">sipp</a> command that does some thing very similar.    </p>
  
  <pre><code>$ 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 
  &lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  &lt;!DOCTYPE scenario SYSTEM "sipp.dtd"&gt;
  
  &lt;scenario name="Options"&gt;
  
    &lt;send retrans="500" start_rtd="opt-timer"&gt;
      &lt;![CDATA[
  
        OPTIONS sip:[remote_ip] SIP/2.0
        Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
        Max-Forwards: 70
        To: &lt;sip:[service]@[remote_ip]&gt;
        From: &lt;sip:[service]@[remote_ip]&gt;;tag=[call_number]
        Call-ID: [call_id]
        CSeq: [cseq] OPTIONS
        Contact: &lt;sip:[service]@[local_ip]:[local_port];transport=[transport]&gt;
        Accept: application/sdp
        Content-Length: 0
  
      ]]&gt;
    &lt;/send&gt;
  
    &lt;recv response="200" rrs="true" rtd="opt-timer"&gt;&lt;/recv&gt;
  
  
    &lt;!-- definition of the response time repartition table (unit is ms)   --&gt;
    &lt;ResponseTimeRepartition value="10, 20, 30, 40, 50, 100, 150, 200"/&gt;
  
    &lt;!-- definition of the call length repartition table (unit is ms)     --&gt;
    &lt;CallLengthRepartition value="10, 50, 100, 500, 1000, 5000, 10000"/&gt;
  
  &lt;/scenario&gt;
  </code></pre>
  
  <p>Which asterisk correctly produces a response to </p>
  
  <pre><code>&lt;--- SIP read from UDP:127.0.0.1:5061 ---&gt;
  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: &lt;sip:service@127.0.0.1&gt;
  From: &lt;sip:service@127.0.0.1&gt;;tag=1
  Call-ID: 1-19903@127.0.1.1
  CSeq: 1 OPTIONS
  Contact: &lt;sip:service@127.0.1.1:5061;transport=UDP&gt;
  Accept: application/sdp
  Content-Length: 0
  
  &lt;-------------&gt;
  --- (10 headers 0 lines) ---
  Sending to 127.0.0.1:5061 (NAT)
  Looking for s in public (domain 127.0.0.1)
  
  &lt;--- Transmitting (NAT) to 127.0.0.1:5061 ---&gt;
  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: &lt;sip:service@127.0.0.1&gt;;tag=1
  To: &lt;sip:service@127.0.0.1&gt;;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: &lt;sip:127.0.0.1:5060&gt;
  Accept: application/sdp
  Content-Length: 0
  
  
  &lt;------------&gt;
  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
  </code></pre>
  
  <p>What I can see is that SIP is doing some other work in the background to initialise a SIP dialog.
  I think that is what is missing in this example code above.
  As asterisk correctly starts Transmitting to a response to the client sipp is creating. </p>