所以我在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))
有人可以帮帮我吗?
答案 0 :(得分:1)
您正在打印的内容已编码。使用.decode()打印实际的字符串。
while 1:
print(s.recv(30).decode())