1. <tfoot id='WohCA'></tfoot>
  2. <i id='WohCA'><tr id='WohCA'><dt id='WohCA'><q id='WohCA'><span id='WohCA'><b id='WohCA'><form id='WohCA'><ins id='WohCA'></ins><ul id='WohCA'></ul><sub id='WohCA'></sub></form><legend id='WohCA'></legend><bdo id='WohCA'><pre id='WohCA'><center id='WohCA'></center></pre></bdo></b><th id='WohCA'></th></span></q></dt></tr></i><div id='WohCA'><tfoot id='WohCA'></tfoot><dl id='WohCA'><fieldset id='WohCA'></fieldset></dl></div>
      <bdo id='WohCA'></bdo><ul id='WohCA'></ul>
    <legend id='WohCA'><style id='WohCA'><dir id='WohCA'><q id='WohCA'></q></dir></style></legend>

    <small id='WohCA'></small><noframes id='WohCA'>

    1. 通过 Socket 发送和接收数组

      Sending and Receiving arrays via Sockets(通过 Socket 发送和接收数组)

    2. <small id='Ca7kS'></small><noframes id='Ca7kS'>

      <i id='Ca7kS'><tr id='Ca7kS'><dt id='Ca7kS'><q id='Ca7kS'><span id='Ca7kS'><b id='Ca7kS'><form id='Ca7kS'><ins id='Ca7kS'></ins><ul id='Ca7kS'></ul><sub id='Ca7kS'></sub></form><legend id='Ca7kS'></legend><bdo id='Ca7kS'><pre id='Ca7kS'><center id='Ca7kS'></center></pre></bdo></b><th id='Ca7kS'></th></span></q></dt></tr></i><div id='Ca7kS'><tfoot id='Ca7kS'></tfoot><dl id='Ca7kS'><fieldset id='Ca7kS'></fieldset></dl></div>
      <tfoot id='Ca7kS'></tfoot>
        <legend id='Ca7kS'><style id='Ca7kS'><dir id='Ca7kS'><q id='Ca7kS'></q></dir></style></legend>

                <tbody id='Ca7kS'></tbody>
              • <bdo id='Ca7kS'></bdo><ul id='Ca7kS'></ul>
                本文介绍了通过 Socket 发送和接收数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否可以使用 Python 通过 UDP 套接字发送数组?我正在使用 Python 2.5 并尝试发送一个简单的数组,但它不起作用.它可以成功发送数组,但是当我尝试用数组的一个项目打印它时,程序崩溃了.我不确定错误是什么,因为我采取了将数据转换为数组的预防措施,但它不起作用.希望我尽可能清楚地解释了这个问题.我将不胜感激!

                Is it possible to send an array through UDP Sockets using Python? I am using Python 2.5 and trying to send a simple array but it's not working. It can send the array successfully but when I try to print it with an item of the array the program crashes. I'm not sure what the error is as I take the precaution of converting the data into an array but it's not working. Hope I explained the problem as clearly as possible. I would appreciate the help!

                # 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 "
                ",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 "
                Received message '", L[1],"'"
                
                # Close socket
                UDPSock.close()
                

                推荐答案

                eval 做的事情与你想的完全不同.

                eval is doing something completely different than what you think.

                要通过网络发送数据,您需要将其序列化为字节数组,然后反序列化返回.在 Python 中,大多数对象的序列化可以通过 pickle 模块完成:

                To send data over network, you need to serialize it into an array of bytes, then deserialize it back. In Python, serialization of most objects can be done via pickle module:

                if (UDPSock.sendto( pickle.dumps(a), addr)):
                

                反序列化:

                data,addr = UDPSock.recvfrom(buf)
                L = pickle.loads(data)
                print repr(L) # prints array('i', [1, 3, 2])
                

                这篇关于通过 Socket 发送和接收数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                  <tbody id='QHepB'></tbody>

                <small id='QHepB'></small><noframes id='QHepB'>

                  <legend id='QHepB'><style id='QHepB'><dir id='QHepB'><q id='QHepB'></q></dir></style></legend>
                  1. <tfoot id='QHepB'></tfoot>
                          <bdo id='QHepB'></bdo><ul id='QHepB'></ul>
                          <i id='QHepB'><tr id='QHepB'><dt id='QHepB'><q id='QHepB'><span id='QHepB'><b id='QHepB'><form id='QHepB'><ins id='QHepB'></ins><ul id='QHepB'></ul><sub id='QHepB'></sub></form><legend id='QHepB'></legend><bdo id='QHepB'><pre id='QHepB'><center id='QHepB'></center></pre></bdo></b><th id='QHepB'></th></span></q></dt></tr></i><div id='QHepB'><tfoot id='QHepB'></tfoot><dl id='QHepB'><fieldset id='QHepB'></fieldset></dl></div>