在 python3 中比较字符串和解码后的 unicode

发布于 2024-10-07 10:43:01 字数 1135 浏览 2 评论 0原文

我正在做一些套接字/选择编程,我的事件之一是由传入的字节字符串 'OK' 触发的。我使用 utf_8 对从服务器发送的所有内容进行编码并在客户端上对其进行解码。但是,我的客户比较不起作用,并且我的 if 语句的计算结果永远不会为 true。这是有问题的代码:

服务器端:

def broadcast_string(self, data, omit_sock): # broadcasts data utf_8 encoded to all socks
    for sock in self.descriptors:
        if sock is not self.server and sock is not omit_sock:
            sock.send(data.encode('utf_8'))
    print(data)

def start_game(self): # i call this to send 'OK'
    data = 'OK'
    self.broadcast_string(data, 0)
    self.new_round()

客户端:

else:   # got data from server
    if data.decode('utf_8') == 'OK': # i've tried substituting this with a var, no luck
        self.playstarted = True
    else:
        sys.stdout.write(data.decode('utf_8') + "\n")
        sys.stdout.flush()

    if self.playstarted is True: # never reached because if statement never True
        command = input("-->")

我已阅读 this 和我想我正在关注它,但显然没有。我什至使用 python shell 完成了这些示例,并让它们评估为 True,但在我运行该程序时却没有。

谢谢!

I'm doing some socket/select programming and one of my events is triggered by the incoming byte string of 'OK'. I'm using utf_8 to encode everything sent from the server and decoding it on the client. However, my client comparisons aren't working and my if statement never evaluates to true. Here is the code in question:

Server side:

def broadcast_string(self, data, omit_sock): # broadcasts data utf_8 encoded to all socks
    for sock in self.descriptors:
        if sock is not self.server and sock is not omit_sock:
            sock.send(data.encode('utf_8'))
    print(data)

def start_game(self): # i call this to send 'OK'
    data = 'OK'
    self.broadcast_string(data, 0)
    self.new_round()

Client side:

else:   # got data from server
    if data.decode('utf_8') == 'OK': # i've tried substituting this with a var, no luck
        self.playstarted = True
    else:
        sys.stdout.write(data.decode('utf_8') + "\n")
        sys.stdout.flush()

    if self.playstarted is True: # never reached because if statement never True
        command = input("-->")

I've read this and I think I'm following it but apparently not. I've even done the examples using the python shell and have had them evaluate to True, but not when I run this program.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2024-10-14 10:43:01

TCP 套接字没有消息边界。正如您的最后一条评论所说,您在一个长字符串中收到多条消息。您负责对数据进行排队,直到获得完整的消息,然后将其作为一条完整的消息进行处理。

每次select表示套接字有一些数据要读取时,将数据附加到读取缓冲区,然后检查缓冲区是否包含完整的消息。如果是,则仅从缓冲区前面提取消息并对其进行处理。继续,直到找不到更多完整消息,然后再次调用 select。另请注意,您应该只解码完整的消息,否则您可能会收到部分 UTF-8 多字节字符。

使用 \n 作为消息终止符的粗略示例(无错误处理):

tmp = sock.recv(1000)
readbuf += tmp
while b'\n' in readbuf:
    msg,readbuf = readbuf.split(b'\n',1)
    process(msg.decode('utf8'))

TCP sockets don't have message boundaries. As your last comment says you are getting multiple messages in one long string. You are reponsible for queuing up data until you have a complete message, and then processing it as one complete message.

Each time select says a socket has some data to read, append the data to a read buffer, then check to see if the buffer contains a complete message. If it does, extract just the message from the front of the buffer and process it. Continue until no more complete messages are found, then call select again. Note also you should only decode a complete message, since you might receive a partial UTF-8 multi-byte character otherwise.

Rough example using \n as a message terminator (no error handling):

tmp = sock.recv(1000)
readbuf += tmp
while b'\n' in readbuf:
    msg,readbuf = readbuf.split(b'\n',1)
    process(msg.decode('utf8'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文