pjsua 和 sip 注册:错误=无效 URI (PJSIP_EINVALIDURI)
pjsua and sip registration: error=Invalid URI (PJSIP_EINVALIDURI)
我正在尝试使用 PJSUA 进行 SIP 注册:
import pjsua as pj
import threading
def log_cb(level, str, len):
print str
class MyAccountCallback(pj.AccountCallback):
sem = None
def __init__(self, account):
pj.AccountCallback.__init__(self, account)
def wait(self):
self.sem = threading.Semaphore(0)
self.sem.acquire()
def on_reg_state(self):
if self.sem:
self.sem.release()
lib = pj.Lib()
try:
lib.init(log_cfg = pj.LogConfig(level=0, callback=log_cb))
lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5080))
lib.start()
acc = lib.create_account(pj.AccountConfig(username='79xxxxxxxxx', password='my_pass', domain='multifon.ru', proxy='sbc.multifon.ru'))
acc_cb = MyAccountCallback(acc)
acc.set_callback(acc_cb)
acc_cb.wait()
print "\n"
print "Registration complete, status=", acc.info().reg_status, \
"(" + acc.info().reg_reason + ")"
lib.destroy()
lib = None
except pj.Error, e:
print "Exception: " + str(e)
lib.destroy()
如果我使用 proxy
参数,我会得到以下错误:
Exception: Object: Lib, operation=create_account(), error=Invalid URI
(PJSIP_EINVALIDURI)
我做错了什么?
好的,我找到了解决方案。代理服务器的地址应以IP address
和port number
的形式呈现,并带有sip:
前缀:
acc = lib.create_account(pj.AccountConfig(username='79xxxxxxxxx', password='my_pass', domain='multifon.ru', proxy='sip:193.201.229.35:5060')
此处193.201.229.35
-域地址对应的IP地址sbc.megafon.ru
以及对代码的一些评论:
1) 与其在代理字段中明确指定 IP 地址,不如使用 socket.gethostbyname
:
proxy_ip = socket.gethostbyname('sbc.megafon.ru')
proxy_str = 'sip:%s:%s' % (proxy_ip, port)
acc = lib.create_account(pj.AccountConfig(username=sip_login, password=sip_password,
domain=sip_domain, proxy=proxy_str))
2) 排队
lib.create_transport (pj.TransportType.UDP, pj.TransportConfig (5080))
端口可以设置为0
——操作系统会自动选择空闲端口。
我正在尝试使用 PJSUA 进行 SIP 注册:
import pjsua as pj
import threading
def log_cb(level, str, len):
print str
class MyAccountCallback(pj.AccountCallback):
sem = None
def __init__(self, account):
pj.AccountCallback.__init__(self, account)
def wait(self):
self.sem = threading.Semaphore(0)
self.sem.acquire()
def on_reg_state(self):
if self.sem:
self.sem.release()
lib = pj.Lib()
try:
lib.init(log_cfg = pj.LogConfig(level=0, callback=log_cb))
lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5080))
lib.start()
acc = lib.create_account(pj.AccountConfig(username='79xxxxxxxxx', password='my_pass', domain='multifon.ru', proxy='sbc.multifon.ru'))
acc_cb = MyAccountCallback(acc)
acc.set_callback(acc_cb)
acc_cb.wait()
print "\n"
print "Registration complete, status=", acc.info().reg_status, \
"(" + acc.info().reg_reason + ")"
lib.destroy()
lib = None
except pj.Error, e:
print "Exception: " + str(e)
lib.destroy()
如果我使用 proxy
参数,我会得到以下错误:
Exception: Object: Lib, operation=create_account(), error=Invalid URI (PJSIP_EINVALIDURI)
我做错了什么?
好的,我找到了解决方案。代理服务器的地址应以IP address
和port number
的形式呈现,并带有sip:
前缀:
acc = lib.create_account(pj.AccountConfig(username='79xxxxxxxxx', password='my_pass', domain='multifon.ru', proxy='sip:193.201.229.35:5060')
此处193.201.229.35
-域地址对应的IP地址sbc.megafon.ru
以及对代码的一些评论:
1) 与其在代理字段中明确指定 IP 地址,不如使用 socket.gethostbyname
:
proxy_ip = socket.gethostbyname('sbc.megafon.ru')
proxy_str = 'sip:%s:%s' % (proxy_ip, port)
acc = lib.create_account(pj.AccountConfig(username=sip_login, password=sip_password,
domain=sip_domain, proxy=proxy_str))
2) 排队
lib.create_transport (pj.TransportType.UDP, pj.TransportConfig (5080))
端口可以设置为0
——操作系统会自动选择空闲端口。