Python Socket 从服务器接收不一致的消息 [英] Python Socket is receiving inconsistent messages from Server

查看:41
本文介绍了Python Socket 从服务器接收不一致的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对网络非常陌生,我使用 Python Socket 库连接到正在传输位置数据流的服务器.

So I am very new to networking and I was using the Python Socket library to connect to a server that is transmitting a stream of location data.

这是使用的代码.

import socket

BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((gump.gatech.edu, 756))

try:
    while (1):
        data = s.recv(BUFFER_SIZE).decode('utf-8')
        print(data)
except KeyboardInterrupt:
    s.close()

问题在于数据以不一致的形式到达.

The issue is that the data arrives in inconsistent forms.

大多数时候它以正确的形式到达,如下所示:

Most of the times it arrives in the correct form like this:

2016-01-21 22:40:07,441,-84.404153,33.778685,5,3

但有时它可以像这样分成两行:

Yet other times it can arrive split up into two lines like so:

2016-01-21

22:40:07,404,-84.396004,33.778085,0,0

有趣的是,当我使用 Putty 建立到服务器的原始连接时,我只会得到正确的形式,而不会得到拆分.所以我想一定有什么事情正在分裂消息.或者 Putty 正在做的事情总是正确地组装它.

The interesting thing is that when I establish a raw connection to the server using Putty I only get the correct form and never the split. So I imagine that there must be something happening that is splitting the message. Or something Putty is doing to always assemble it correctly.

我需要的是变量 data 始终包含正确的行.知道如何做到这一点吗?

What I need is for the variable data to contain the proper line always. Any idea how to accomplish this?

推荐答案

最好将套接字视为连续的数据流,它可能以点点滴滴或泛滥的方式到达.

It is best to think of a socket as a continuous stream of data, that may arrive in dribs and drabs, or a flood.

特别是,接收者的工作是将数据分解成它应该包含的记录",套接字不会神奇地知道如何为您做到这一点.这里的记录是行,所以你必须自己读取数据并拆分成行.

In particular, it is the receivers job to break the data up into the "records" that it should consist of, the socket does not magically know how to do this for you. Here the records are lines, so you must read the data and split into lines yourself.

您不能保证单个 recv 将是一个完整的行.可能是:

You cannot guarantee that a single recv will be a single full line. It could be:

  • 只是一行的一部分;
  • 或几行;
  • 或者,最有可能的是几行和另一行.

尝试类似:(未经测试)

Try something like: (untested)

# we'll use this to collate partial data
data = ""

while 1:
    # receive the next batch of data
    data += s.recv(BUFFER_SIZE).decode('utf-8')

    # split the data into lines
    lines = data.splitlines(keepends=True)

    # the last of these may be a part line
    full_lines, last_line = lines[:-1], lines[-1]

    # print (or do something else!) with the full lines
    for l in full_lines:
        print(l, end="")

    # was the last line received a full line, or just half a line?
    if last_line.endswith("\n"):
        # print it (or do something else!)
        print(last_line, end="")

        # and reset our partial data to nothing
        data = ""
    else:
        # reset our partial data to this part line
        data = last_line

这篇关于Python Socket 从服务器接收不一致的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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