是否可以通过使用Python的UDP套接字发送数组?我正在使用Python 2.5并试图发送一个简单的数组,但它不起作用。它可以成功发送数组,但是当我尝试使用数组中的项目打印时,程序崩溃。我不确定错误是什么,因为我采取了将数据转换为数组的预防措施,但它不起作用。希望我尽可能清楚地解释问题。我将不胜感激帮助!通过套接字发送和接收数组
# Client program
from socket import *
import numpy
from array import*
# Set the socket parameters
host = "localhost"
port = 21567
buf = 4096
addr = (host,port)
# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)
def_msg = "===Enter message to send to server===";
print "\n",def_msg
a = array('i',[1,3,2])
# Send messages
while (1):
data = raw_input('yes or now')
if data!= "yes":
break
else:
if(UDPSock.sendto(a,addr)):
print "Sending message"
# Close socket
UDPSock.close()
# Server program
from socket import *
# Set the socket parameters
host = "localhost"
port = 21567
buf = 4096
addr = (host,port)
# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
# Receive messages
while 1:
data,addr = UDPSock.recvfrom(buf)
L = eval(data)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", L[1],"'"
# Close socket
UDPSock.close()
This Works,Thank you! – dawnoflife