好的,我有一个连接到服务器的客户端,并且我想知道如何实现接收整个消息的方法,因为我不知道消息的大小。 (假设服务器向我发送了5000个字节(我事先不知道),而我创建的客户端仅接收1024个字节,我如何收集整个消息并将其与我收到的第一条消息连接起来?)
这发生在我身上
mensaje = cliente.recv(1024).decode()
while (): #I don't know how to put the condition (it would be if the message had \ 0) (I don't know if python # has \ 0, to indicate the end of the string.
#In the event that if this is \ 0 in py, that would be the condition, you would have to put a for that
#recess the message until you find \ 0?. A for i in len (message): while i! = "\ 0"
mensaje += cliente.recv(1024).decode()
我的另一个问题是遍历该消息,只是作为分隔符包含空格,我会使用.split,但是我的问题是:
for a in ...: #there is a way to put range (0, len (message.split (""))) (I know this doesn't work, #because I tried it and it gives me an error). I want to loop through the message and after doing the first # iteration compare to with a number or letter. But I don't really know how to do that for, I also # thought of two nested fors, but I don't know if it could overlap
为了更加清楚,我将其放在不加注释的位置:怀疑1.a:.py中没有“ \ 0”
mensaje = cliente.recv(1024).decode()
while .... :
mensaje += cliente.recv(1024).decode()
疑问1.b:.py中有“ \ 0”
mensaje = cliente.recv(1024).decode()
for i in mensaje:
while i != "\0":
mensaje += cliente.recv(1024).decode()
疑问2:当我已经收到完整的消息时,我想遍历该消息,但它是一个字符串,以空格作为分隔符,并且我想计算最多x个符号的单词或数字
for i in range(0, len(mensaje,split(" "))):
o
for i in range(0, len(mensaje)):
for a in mensaje.split(" "):
if a == x:
..............
else:
.............
我尝试这样做:
cont=0
msg = cliente.recv(1024).decode()
for i in msg:
while i != "\0":
msg += cliente.recv(1024).decode()
for a in msg:
for b in msg.split():
if b == "x":
break
else:
cont = cont + 1
但是它什么也没做,服务器也没有给我答案(它应该向我显示一些信息,为什么要发送计数器然后执行recv(1024)并打印消息。(但是什么都不显示)