Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send SIP message using Python sockets

Tags:

python

sip

invite

I need to send SIP message using Python sockets, I've made that client sends something to server but I'm not able to make that client sends a SIP message INVITE to server

#!/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()

Do you have some idea?

like image 794
Damian Silkowski Avatar asked Apr 22 '26 04:04

Damian Silkowski


2 Answers

According to RFC 3261:

  1. Each header must start with a newline and there must be no whitespaces before header name.
  2. Minimal SIP request must contain To, From, CSeq, Call-ID, Max-Forwards and Via headers.

This means that you correctly added newline before To header, but your answer also incorrectly contains Froma header instead of From. Also, header P-Served-User is generally unnecessary.

like image 98
Andriy Makukha Avatar answered Apr 23 '26 18:04

Andriy Makukha


Ok I know, maybe it will help someone.

Proper format SIP message is as follows:

INVITE sip:user11@whatever SIP/2.0
To: <to>
Call-ID: <call_id>
<empty line>
body

So,

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 is very important without empty spaces

like image 44
Damian Silkowski Avatar answered Apr 23 '26 16:04

Damian Silkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!