有一个随机 'b' 不应该在那里

There a random 'b' that shouldn't be there

所以我在 python 中玩 UDP 通信,我终于得到了一些消息传递的东西,但在客户端,有一个随机的 'b' 这是我的代码

udpServer.py:

import socket
import time
import os
import getpass

userNonByt = getpass.getuser()
username = bytes(userNonByt, 'utf-8')
HOST = "localhost"
PORT =  5454
HOST = bytes(HOST, 'utf-8')
bytes(PORT)
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

def setmsg():
    os.system('cls')
    #asks user for there text to send
    data = input("Send: ")
    #converts there text to bytes so it can be sent in the UDP Packet
    text = bytes(data, 'utf-8')
    #sends the users text
    s.sendto(username,(HOST,PORT))
    s.sendto((text),(HOST,PORT))
    #brings you back to the begining of this function
    setmsg()


setmsg()

udpClient.py:

import socket


HOST = 'localhost'
PORT =  5454
HOST = bytes(HOST, 'utf-8')

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

s.bind((HOST,PORT))

while 1:
    print (s.recv(30))

有人可以帮我吗?

您正在打印的内容已编码。使用 .decode() 打印实际字符串。

while 1:
    print(s.recv(30).decode())