python socket双向通信_python中的双向实时通信

一种选择是将流分割成消息,以交错时钟和音频数据。消息可以是一个头,其中包含类型指示符和随后使用struct模块编码的字节数。这段代码是未经测试的,实际上是伪代码,但类似。。。在import struct

def send_audio(audio_socket, audio_data_fragment):

"""send the next bit of audio. if fragment size is large,

the next clock send will be delayed"""

audio_socket.sendall(struct,pack('!bL', 0, len(audio_data_fragment))

audio_socket.sendall(audio_data_fragment)

def send_clock(audio_socket, clock_val):

"""send the next clock info. just guessing that its an 8 byte

binary number as an example"""

audio_socket.sendall(struct.pack('!bQ', 1, clock_val))

def _recvall(sock, sz):

buf = ''

while len(buf) < sz:

buf += sock.recv(sz - len(buf))

return buf

def recv(audio_socket):

"""recv next message and return a tuple of (type, data)"""

msg_type = struct.unpack('b', audio_socket.recv(1)))

if msg_type == 0:

audio_len = struct.unpack('!L', _recvall(audio_socket, 4))

audio = _recvall(audio_socket, audio_len)

return 'audio', audio

elif msg_type == 1:

clock = struct.unpack('!Q', _recvall(audio_socket, 8))

return 'clock', clock

另一个选择是选择一个为您生成消息的现有系统。它可以是python标准xmlrpc模块,也可以是zeromq、协议缓冲区或trift。有很多选择。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值