【发布时间】:2016-05-31 19:48:44
【问题描述】:
所以我在 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))
有人可以帮帮我吗?
【问题讨论】:
-
切换到 Python 2.7,
b(b"...")将消失 ;-) -
b"..."仅表示它是bytestring而不是unicode string... -
递归不能替代循环。
-
对“随机 b”的描述尤其缺乏信息。好在我们善于猜测,否则我们会在几个小时前关闭这篇文章。