【发布时间】:2015-06-08 04:27:20
【问题描述】:
在 python 3.4.2 中实现客户端-服务器 UDP 通信时,遇到了解码从客户端发送到服务器的 base64 编码数字的问题。
客户端代码:
x = 10
y = 15
z = x + y
print("z value ",z)
encoded = base64.b64encode(bytes(str(z), 'ascii'))
print('encoded z', encoded)
sock = socket.socket(socket.AF_INET, #Internet
socket.SOCK_DGRAM) #UDP
sock.sendto(encoded, (UDP_IP, UDP_PORT))
服务器:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
z, addr = sock.recvfrom(1024) #buffer size is 1024 bytes
decode = base64.b64decode(bytes(str(z),'ascii'))
print("Received message:", z, decode)
if not z:break
客户端的“编码值”和服务器端接收的“z”值相同。 但是在 decode = base64.b64decode(bytes(str(z),'ascii')) 上 如何在服务器端正确解码和显示 z 值?
请指教。
谢谢。
【问题讨论】:
标签: python sockets python-3.x client server