简体   繁体   English

如何使用 Python 套接字发送 SIP 消息

[英]How to send SIP message using Python sockets

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我需要使用 Python 套接字发送 SIP 消息,我已经让该客户端向服务器发送了一些东西,但我无法让该客户端向服务器发送 SIP 消息 INVITE

#!/usr/bin/python

import socket

R_IP = '192.168.2.1'
R_PORT = 5060 

message = 'INVITE sip:user1110000000350@.com SIP/2.0 To: <sip:user4110000000350@whatever.com>\x0d\x0aFrom: sip:user9990000000000@rider.com;tag=R400_BAD_REQUEST;taag=4488.1908442942.0\x0d\x0aP-Served-User: sip:user4110000000350@whatever.com\x0d\x0aCall-ID: 00000000-00001188-71C0873E-0@10.44.40.47\x0d\x0aCSeq: 1 INVITE\x0d\x0aContact: sip:user9990000000000@rider.com\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?你有什么想法吗?

According to RFC 3261 :根据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.最小的 SIP 请求必须包含ToFromCSeqCall-IDMax-ForwardsVia标头。

This means that you correctly added newline before To header, but your answer also incorrectly contains Froma header instead of From .这意味着您在To标题之前正确添加了换行符,但您的答案也错误地包含Froma标题而不是From Also, header P-Served-User is generally unnecessary.此外,标题P-Served-User通常是不必要的。

Ok I know, maybe it will help someone.好的,我知道,也许它会帮助某人。

Proper format SIP message is as follows:正确格式的 SIP 消息如下:

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

So,所以,

message = 'INVITE sip:user1110000000350@whatever.com SIP/2.0\r\nTo: <sip:user4110000000350@whatever.com>\r\nFroma: sip:user9990000000000@rider.com;tag=R400_BAD_REQUEST;taag=4488.1908442942.0\r\nP-Served-User: sip:user4110000000350@whatever.com\r\nCall-ID: 00000000-00001188-71C0873E-0@10.44.40.47\r\nCSeq: 1 INVITE\r\nContact: sip:user9990000000000@rider.com\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 \\r\\n没有空格很重要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM