[英]Python - How to generate long, fixed length, valid byte strings?
I'm trying to create a socket-based client which can send simulated Call Records to a receiver.我正在尝试创建一个基于套接字的客户端,它可以将模拟呼叫记录发送到接收器。 The socket client is complete, now I need to work on the payload generator.套接字客户端已完成,现在我需要处理有效负载生成器。 The call record looks like this:通话记录如下所示:
CDR = b'173600009 3052 334088888 1001 0 #500 0 00000000 0 0 \r\n\x00\x00\x00'
Each space is counted, meaning that each bit in this string belongs to a "field" Spaces just mean no data for that particular field.每个空格都被计算在内,这意味着该字符串中的每一位都属于一个“字段”,空格仅表示该特定字段没有数据。
I'm looking for a proper way to generate records like this, but make it simple enough where I can specify a range of values for each field.我正在寻找一种正确的方法来生成这样的记录,但要让它足够简单,我可以为每个字段指定一个值范围。
Basically:基本上:
timestamp = CDR[0:4]
call_party = CDR[12:18]
... etc ... 等等
then something like然后像
for i in [list_of_call_party]:
call_party = i
finally:最后:
join( timestamp, call_party, etc), etc to create a new VALID record of the proper length.
I just need a nudge in the right direction to make this Call record generator.我只需要朝着正确的方向轻推即可制作此通话记录生成器。
Thanks!谢谢!
The easiest way is probably an encode method...最简单的方法可能是编码方法......
def encode(timestamp=0, ...):
return '%-*s...' % 4, timestamp, ...
You can add a method for decode as well...您也可以添加解码方法...
def decode(msg):
return msg[0:4], ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.