处理以换行符结尾的套接字数据 [英] process socket data that ends with a line break

查看:52
本文介绍了处理以换行符结尾的套接字数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理需要 var data 以换行符 \n 结尾的套接字连接的最佳方法是什么?我正在使用下面的代码,但有时 tcp 数据包会 chunked 并且需要很长时间才能匹配 data.endswith("\n").我还尝试了其他方法,例如保存最后一行,如果它不以 \n 结尾,并将其附加到下一个循环中的 data .但这也不起作用,因为多个数据包被分块并且第一和第二部分不匹配.我无法控制另一端,它基本上发送以 \r\n 结尾的多行.

What is the best approach to process a socket connection where I need var data to end with a line break \n? I'm using the code below but sometimes the tcp packets get chunked and it takes a long time to match data.endswith("\n"). I've also tried other approaches, like saving the last line if it doesn't end with \n and append it to dataon the next loop. but this also doesn't work because multiple packets get chunked and the 1st and 2nd part don't match. I've no control over the other end, it basically sends multiple lines that end in \r\n.

欢迎提出任何建议,因为我对套接字连接的了解不多.

Any suggestion will be welcome, as I don't have much knowledge on socket connections.

def receive_bar_updates(s):
    global all_bars
    data = ''
    buffer_size = 4096
    while True:
        data += s.recv(buffer_size)
        if not data.endswith("\n"):
            continue
        lines = data.split("\n")
        lines = filter(None, lines)
        for line in lines:
            if line.startswith("BH") or line.startswith("BC"):
                symbol = str(line.split(",")[1])
                all_bars[symbol].append(line)
                y = Thread(target=proccess_bars, kwargs={'symbol': symbol})
                y.start()
        data = ""

normal"示例data:

line1\r\n
line2\r\n
line3\r\n

chunked data 示例:

line1\r\n
line2\r\n
lin

推荐答案

如果您想将原始输入作为行处理,那么 io 模块是您的朋友,因为它会按行进行数据包的低级组装.

If you have a raw input that you want to process as line, the io module is your friend because it will do the low level assembling of packets in lines.

您可以使用:

class SocketIO(io.RawIOBase):
    def __init__(self, sock):
        self.sock = sock
    def read(self, sz=-1):
        if (sz == -1): sz=0x7FFFFFFF
        return self.sock.recv(sz)
    def seekable(self):
        return False

它比 endswith('\n') 更健壮,因为如果一个数据包包含一个嵌入的换行符 ('ab\ncd'),io 模块将正确处理它.您的代码可能会变成:

It is more robust than endswith('\n') because if one packet contains an embedded newline ('ab\ncd'), the io module will correctly process it. Your code could become:

def receive_bar_updates(s):
    global all_bars
    data = ''
    buffer_size = 4096
    fd = SocketIO(s)  # fd can be used as an input file object

    for line in fd:
        if should_be_rejected_by_filter(line): continue # do not know what filter does...
        if line.startswith("BH") or line.startswith("BC"):
            symbol = str(line.split(",")[1])
            all_bars[symbol].append(line)
            y = Thread(target=proccess_bars, kwargs={'symbol': symbol})
            y.start()

这篇关于处理以换行符结尾的套接字数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆