完成定长头报文的封装和解封

1. [代码]lmcc_common.py     

001 #!/usr/bin/env python
002  
003 "Common Tools module for LMCC project"
004  
005 import struct
006 import ctypes
007  
008  
009 def fieldcode(fieldname):
010     "return field code by field name"
011     field_2_code = {}
012     field_2_code['SESSION_KEY'= '2'
013     field_2_code['STATUS'= '3'
014     field_2_code['USER_ID'= '4'
015     field_2_code['NAS_IP'= '5'
016     field_2_code['NAS_PORT'= '6'
017     field_2_code['NAS_PORT_TYPE'= '7'
018     field_2_code['SERVICE_TYPE'= '8'
019     field_2_code['SERVICE_PORT'= '9'
020     field_2_code['SERVICE_IP'= '10'
021     field_2_code['SESSION_START_TIME'= '11'
022     field_2_code['SESSION_STATUS'= '12'
023     field_2_code['SESSION_TIME'= '13'
024     field_2_code['MAX_CONNS'= '14'
025     field_2_code['USER_TYPE'= '15'
026     field_2_code['SESSION_ID'= '16'
027     field_2_code['CALLER'= '17'
028     field_2_code['CALLEE'= '18'
029     field_2_code['RESERVED1'= '19'
030     field_2_code['RESERVED2'= '20'
031     field_2_code['RESERVED3'= '21'
032     field_2_code['RESERVED4'= '22'
033     field_2_code['LAST_ALIVE_TIME'= '23'
034     return field_2_code.get(fieldname)
035  
036  
037 def htons(num):
038     return struct.pack('!H', num)
039  
040  
041 def htonl(num):
042     return struct.pack('!I', num)
043  
044  
045 def ntohs(data):
046     return struct.unpack('!H', data)[0]
047  
048  
049 def ntohl(data):
050     return struct.unpack('!I', data)[0]
051  
052  
053 def ntohb(data):
054     return struct.unpack('B', data)[0]
055  
056  
057 def wraptlv(tlvs):
058     "wrap tlv"
059     if len(tlvs) == 0:
060         return [0, '']
061     bodylen = 0
062     body = ''
063     for item in tlvs:
064         attrtype = int(item[0])
065         attrvalue = item[1]
066         attrlen = len(attrvalue)
067         formatstr1 = "%ds" % attrlen
068         buf = ctypes.create_string_buffer(attrlen + 3)
069         offset = 0
070         struct.pack_into("B", buf, offset, attrtype)
071         offset = offset + 1
072         struct.pack_into("!H", buf, offset, attrlen + 3)
073         offset = offset + 2
074         struct.pack_into(formatstr1, buf, offset, attrvalue)
075         body = body + buf.raw
076         bodylen = bodylen + len(buf.raw)
077     return [bodylen, body]
078  
079  
080 def unwrapfieldtlv(data):
081     "unwrap field tlv"
082     return data
083  
084  
085 def unwraprecordtlv(data):
086     "unwrap record tlv"
087     record = []
088     offset = 0
089     while offset < len(data):
090         attrtypedata = data[offset:offset + 1]
091         attrtype = ntohb(attrtypedata)
092         offset = offset + 1
093         attrlendata = data[offset:offset + 2]
094         attrlen = ntohs(attrlendata)
095         offset = offset + 2
096         if attrlen - 3 0:
097             attrvaluedata = data[offset:offset + attrlen - 3]
098             offset = offset + attrlen - 3
099             if attrtype == 0:
100                 attrvalue = unwraprecordtlv(attrvaluedata)
101             else:
102                 attrvalue = unwrapfieldtlv(attrvaluedata)
103             record.append((str(attrtype), attrvalue))
104         else:
105             record.append((str(attrtype), ''))
106     return record
107  
108  
109 def unwraptlv(data):
110     "unwrap tlv"
111     result = []
112     offset = 0
113     while offset < len(data):
114         attrtypedata = data[offset:offset + 1]
115         attrtype = ntohb(attrtypedata)
116         offset = offset + 1
117         attrlendata = data[offset:offset + 2]
118         attrlen = ntohs(attrlendata)
119         offset = offset + 2
120         if attrlen - 3 0:
121             attrvaluedata = data[offset:offset + attrlen - 3]
122             offset = offset + attrlen - 3
123             if attrtype == 0:
124                 attrvalue = unwraprecordtlv(attrvaluedata)
125             else:
126                 attrvalue = unwrapfieldtlv(attrvaluedata)
127             result.append((str(attrtype), attrvalue))
128         else:
129             result.append((str(attrtype), ''))
130     return result
131  
132  
133 def sendpacket(sock, serialno, msgtype, errcode, tlvs):
134     "wrap data packet and send it"
135     totollen = 0
136     bodylen = 0
137     offset = 0
138     body = ''
139     wraptlvdata = wraptlv(tlvs)
140     bodylen = wraptlvdata[0]
141     body = wraptlvdata[1]
142     totollen = bodylen + 12
143     formatstr = "%ds" % bodylen
144     data = ctypes.create_string_buffer(totollen)
145     offset = 0
146     struct.pack_into("!I", data, offset, totollen)
147     offset = offset + 4
148     struct.pack_into("!I", data, offset, serialno)
149     offset = offset + 4
150     struct.pack_into("B", data, offset, msgtype)
151     offset = offset + 1
152     struct.pack_into("!H", data, offset, errcode)
153     offset = offset + 2
154     struct.pack_into("B", data, offset, 0)
155     offset = offset + 1
156     if bodylen > 0:
157         struct.pack_into(formatstr, data, offset, body)
158     sock.send(data)
159  
160  
161 def receivepacket(sock):
162     "receive packet and unwrap it"
163     data = sock.recv(4)
164     totallen = ntohl(data)
165     data = sock.recv(4)
166     serialno = ntohl(data)
167     data = sock.recv(1)
168     msgtype = ntohb(data)
169     data = sock.recv(2)
170     errcode = ntohs(data)
171     data = sock.recv(1)
172     attrnum = ntohb(data)
173     bodylen = totallen - 12
174     if bodylen > 0:
175         data = sock.recv(bodylen)
176         wraptlvdata = unwraptlv(data)
177         return [serialno, msgtype, errcode, attrnum, wraptlvdata]
178     return [serialno, msgtype, errcode, attrnum]
179  
180  
181 if __name__ == '__main__':
182     print 'test!'

2. [代码]lmcc_batchinsert.py     

01 #!/usr/bin/env python
02  
03 import socket
04 import lmcc_common
05  
06  
07 host = '10.1.252.189'
08 port = 2000
09  
10 #open socket to remote server
11 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12 s.connect((host, port))
13 s.settimeout(5)
14  
15 #open the file of test data, and send data to remote server
16 fp = open('testdata.txt''r')
17 for eachLine in fp:
18     eachLine = eachLine.strip()
19     fields = eachLine.split(',')
20     print "handle line:[", eachLine, "]"
21     serialno = 1
22     msgtype = 1
23     errcode = 0
24     tlvs = []
25     for field in fields:
26         keyvalue = field.split('=')
27         key = lmcc_common.fieldcode(keyvalue[0].strip())
28         value = keyvalue[1].strip()
29         tlvs.append([key, value])
30     lmcc_common.sendpacket(s, serialno, msgtype, errcode, tlvs)
31     result = lmcc_common.receivepacket(s)
32     if result[2== 0:
33         print "success!"
34     print "done!"
35 fp.close()
36  
37 #close socket to remote server
38 s.close()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值