Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
socket编程中的Base64解码python帮助_Python_Sockets_Python 3.x_Client_Server - Fatal编程技术网

socket编程中的Base64解码python帮助

socket编程中的Base64解码python帮助,python,sockets,python-3.x,client,server,Python,Sockets,Python 3.x,Client,Server,在Python3.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

在Python3.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
服务器:

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(字节(str(z),'ascii'))行被击中 如何在服务器端正确解码和显示z值

请告知


谢谢。

您的服务器正在进行一些额外的工作。在客户端中,encoded是base64编码的字节字符串。这是您发送到套接字的内容,也是服务器输出的内容;base64编码的字节字符串。您的代码很好,但是您不小心通过修改z将消息弄错了。这是您的服务器代码,已更正:

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(z)
  print("Received message:", z, decode)
  if not z:break