在 python3 中比较字符串和解码后的 unicode
我正在做一些套接字/选择编程,我的事件之一是由传入的字节字符串 '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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TCP 套接字没有消息边界。正如您的最后一条评论所说,您在一个长字符串中收到多条消息。您负责对数据进行排队,直到获得完整的消息,然后将其作为一条完整的消息进行处理。
每次
select
表示套接字有一些数据要读取时,将数据附加到读取缓冲区,然后检查缓冲区是否包含完整的消息。如果是,则仅从缓冲区前面提取消息并对其进行处理。继续,直到找不到更多完整消息,然后再次调用select
。另请注意,您应该只解码
完整的消息,否则您可能会收到部分 UTF-8 多字节字符。使用
\n
作为消息终止符的粗略示例(无错误处理):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 callselect
again. Note also you should onlydecode
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):