# -*- coding: iso-8859-1 -*-
#!/usr/bin/python
# Author:Athlong
import struct
import socket
import SGIPConstant
import time
BUFFSIZ=1024
debug=1
SP_CORP_ID=SGIPConstant.SP_CORP_ID
SeqId=1
def Bind(HOST,PORT):
ADDR=(HOST,PORT)
print '**************************************'
print '*SMSW Listen Port: %05d *' % PORT
print '**************************************'
print
SN_ID=SendSN()
try:
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(ADDR)
s=struct.pack('>i',61)
s+=struct.pack('!L',SGIPConstant.COMMAND_ID_BIND)
s+=struct.pack('!Lii',SP_CORP_ID,SN_ID,0)
s+=struct.pack('b',1)
s+=StringResult('test',20)
s+=StringResult('test',20)
s+=StringResult('',8)
c.send(s)
rcvmsg=c.recv(1024)
if struct.unpack('b',rcvmsg[21:22])[0]==0:
if debug:print '登陆联通网关成功:%s,%s'%(ADDR)
except:
if debug:print '登陆联通网关失败:%s,%s'%(ADDR)
def Bind_Resp(conn,flg,SeqId):
SN_ID=SendSN()
s=struct.pack('>i',29)
s+=struct.pack('!L',SGIPConstant.COMMAND_ID_BIND_RESP)
s+=struct.pack('!Lii',SP_CORP_ID,SN_ID,SeqId)
s+=struct.pack('b',flg)
s+=StringResult('',8)
if debug:print 'Bind_Resp:--------------------OK/r/n'
conn.send(s)
return
def Unbind_Resp(conn,flg,SeqId):
SN_ID=SendSN()
s=struct.pack('>i',29)
s+=struct.pack('!L',SGIPConstant.COMMAND_ID_UNBIND_RESP)
s+=struct.pack('>Lii',SP_CORP_ID,SN_ID,SeqId)
s+=struct.pack('b',flg)
s+=StringResult('',8)
if debug:print 'UnBind_Resp:--------------------OK/r/n'
conn.send(s)
return
def Deliver(rcvres):
optmsg=rcvres[2]
UserNumber=optmsg[:21] #长度21,类型TEXT,用户手机号前面有'86'
SPNumber=optmsg[21:42] #SP接入号,类型TEXT
TP_pid=struct.unpack('b',optmsg[42:43]) #GSM协议类型,类型Int详细解释请参考GSM03.40中的9.2.3.9
TP_udhi=struct.unpack('b',optmsg[43:44]) #GSM协议类型。详细解释请参考GSM03.40中的9.2.3.23,仅使用1位
MessageCoding=struct.unpack('b',optmsg[44:45]) #短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码
MessageLength=struct.unpack('>i',optmsg[45:49]) #短消息的长度
MessageContent=optmsg[49:49+MessageLength[0]] #短消息的内容
return (1,UserNumber.split('/0')[0],SPNumber.split('/0')[0],TP_pid,MessageCoding,MessageLength,MessageContent)
def Deliver_Resp(conn,flg,SeqId):
SN_ID=SendSN()
s=struct.pack('>i',29)
s+=struct.pack('!L',SGIPConstant.COMMAND_ID_DELIVER_RESP)
s+=struct.pack('>Lii',SP_CORP_ID,SN_ID,SeqId)
s+=struct.pack('b',0)
s+=StringResult('',8)
if debug:print 'Deliver_Resp:-----------------OK/r/n'
conn.send(s)
return
def Submit(rcvres):
optmsg=rcvres[2]
SPNumber=optmsg[:21] #SP接入号,类型TEXT
ChargeNumber=optmsg[21:42]#付费号码,手机号码前加"86"国别标志
UserCount=struct.unpack('b',optmsg[42:43])#接收短消息的手机数量,取值范围1至100
Count=43+21*UserCount[0]
UserNumber=optmsg[43:Count]#接收该短消息的手机号,该字段重复UserCount指定的次数,手机号码前加"86"国别标志
CorpId=optmsg[Count:Count+5]#企业代码,取值范围0-99999
ServiceType=optmsg[Count+5:Count+15]#业务代码,由SP定义
FeeType=struct.unpack('b',optmsg[Count+15:Count+16])#计费类型
FeeValue=optmsg[Count+16:Count+22]#计费值 取值范围0-99999
GivenValue=optmsg[Count+22:Count+28]#取值范围0-99999,赠送用户的话费,单位为分
AgentFlag=struct.unpack('b',optmsg[Count+28:Count+29])#代收费标志,0:应收;1:实收
MorelatetoMTFlag=struct.unpack('b',optmsg[Count+29:Count+30])#引起MT消息的原因
Priority=struct.unpack('b',optmsg[Count+30:Count+31])#优先级0-9从低到高,默认为0
ExpireTime=optmsg[Count+31:Count+37]#短消息寿命的终止时间,如果为空,表示使用短消息中心的缺省值
ScheduleTime=optmsg[Count+37:Count+43]#短消息定时发送的时间,如果为空,表示立刻发送该短消息
ReportFlag=struct.unpack('b',optmsg[Count+43:Count+44])#状态报告标记0-该条消息只有最后出错时要返回状态报告1-该条消息无论最后是否成功都要返回状态报告
TP_pid=struct.unpack('b',optmsg[Count+44:Count+45])#GSM协议类型
TP_udhi=struct.unpack('b',optmsg[Count+45:Count+46])#GSM协议类型
MessageCoding=struct.unpack('b',optmsg[Count+46:Count+47])#短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码
MessageType=struct.unpack('b',optmsg[Count+47:Count+48])#信息类型:0-短消息信息其它:待定
MessageLength=struct.unpack('>i',optmsg[Count+48:Count+52])#短消息的长度
MessageContent=optmsg[Count+52:Count+52+MessageLength[0]]#短消息的内容
Reserve=optmsg[Count+52+MessageLength[0]:]#扩展保留
return(1,SPNumber.split('/0')[0],ChargeNumber.split('/0')[0],UserCount,UserNumber.split('/0')[0],CorpId,ServiceType.split('/0')[0],FeeType[0],FeeValue.split('/0')[0],GivenValue,AgentFlag,MorelatetoMTFlag,Priority,ExpireTime,ScheduleTime,ReportFlag,TP_pid,TP_udhi,MessageCoding,MessageType,MessageLength,MessageContent)
def Submit_Resp(conn,flg,SeqId):
SN_ID=SendSN()
s=struct.pack('>i',29)
s+=struct.pack('!L',SGIPConstant.COMMAND_ID_SUBMIT_RESP)
s+=struct.pack('>Lii',SP_CORP_ID,SN_ID,SeqId)
s+=struct.pack('b',flg)
s+=StringResult('',8)
if debug:print 'Submit_Resp:-----------------OK/r/n'
conn.send(s)
return
def StringResult(str,num): #补'/0'转网络字节算法
j=0
g=[]
s=''
while 1:
if (len(str)<num):
str=str+'/0'
else:
break
g=str
while 1:
if j<len(g):
s+=struct.pack('>c',g[j])
j+=1
else:
break
return s
def SendSN(): #序列号算法
sn=time.strftime('%Y-%m-%d%H%M%S',time.localtime()).split('-')[1]
sn+=time.strftime('%Y-%m-%d%H%M%S',time.localtime()).split('-')[2]
return int(sn)