1. <small id='oWwcJ'></small><noframes id='oWwcJ'>

      2. <tfoot id='oWwcJ'></tfoot><legend id='oWwcJ'><style id='oWwcJ'><dir id='oWwcJ'><q id='oWwcJ'></q></dir></style></legend>

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

        多个写入作为单个处理

        Multiple writes get handled as single one(多个写入作为单个处理)
        • <bdo id='3nJZ5'></bdo><ul id='3nJZ5'></ul>

          1. <legend id='3nJZ5'><style id='3nJZ5'><dir id='3nJZ5'><q id='3nJZ5'></q></dir></style></legend>

              <small id='3nJZ5'></small><noframes id='3nJZ5'>

              <tfoot id='3nJZ5'></tfoot>

                    <tbody id='3nJZ5'></tbody>

                  <i id='3nJZ5'><tr id='3nJZ5'><dt id='3nJZ5'><q id='3nJZ5'><span id='3nJZ5'><b id='3nJZ5'><form id='3nJZ5'><ins id='3nJZ5'></ins><ul id='3nJZ5'></ul><sub id='3nJZ5'></sub></form><legend id='3nJZ5'></legend><bdo id='3nJZ5'><pre id='3nJZ5'><center id='3nJZ5'></center></pre></bdo></b><th id='3nJZ5'></th></span></q></dt></tr></i><div id='3nJZ5'><tfoot id='3nJZ5'></tfoot><dl id='3nJZ5'><fieldset id='3nJZ5'></fieldset></dl></div>
                  本文介绍了多个写入作为单个处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这段代码有问题.我正在打开一个套接字,然后用一个 while 循环来监听它.我从 php 脚本发送数据

                  I have a problem here with this code. I'm opening a socket, then listening to it with a while loop. I send data from a php script with

                  socket_write($sock, $test, $len);
                  

                  它工作得很好,但是当我连续发送多个写入时,Python 脚本将一些写入处理为一次写入.

                  It works very well, but when I send several writes in a row, the Python script handles some of the writes as just one write.

                  import socket
                  
                  HOST = 'localhost' # the host
                  PORT = 12126 # the port
                  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                  s.bind((HOST, PORT))
                  while 1:
                    s.listen(0)
                    conn, addr = s.accept()
                    print 'Connected by', addr
                    while 1:
                        data = conn.recv(1024)
                        if not data: break
                  conn.close()
                  

                  我正在寻找一种方法来监听该端口并获得一个接一个的写入.

                  I'm looking for a way to listen to that port and get one write after another.

                  推荐答案

                  这不是套接字的工作方式.字节输入和字节输出,但必须有一些其他机制来告诉您消息有多大.如果您在套接字的一端发送 50 个字节,然后再发送 75 个字节,然后是 20 个字节,然后调用 recv(100),则您可以从阻塞套接字中获取 1 到 100 个字节的任何值.你负责缓冲recv,直到你有一个完整的消息,你必须定义一个完整的消息是什么.一些选项:

                  That's not the way sockets work. Bytes go in and bytes come out, but there has to be some other mechanism to tell you how big a message is. If you send 50 bytes, then another 75 bytes, then 20 bytes on one end of a socket, and then call recv(100), you could get anywhere from 1 to 100 bytes from a blocking socket. You are responsible for buffering recv's until you have a complete message, and you have to define what a complete message is. Some options:

                  1. 发送固定长度的消息.
                  2. 发送代表消息长度的固定字节数,然后是消息.
                  3. 用标记字节分隔消息.

                  这是一个使用标记字节缓冲接收到的数据的类的示例:

                  Here's an example of a class to buffer received data using a sentinel byte:

                  import socket
                  
                  class Client(object):
                  
                      def __init__(self):
                          self.buffer = ''
                          self.sock = None
                  
                      def connect(self,address):
                          self.buffer = ''
                          self.sock = socket.socket()
                          self.sock.connect(address)
                  
                      def get_msg(self):
                          '''Append raw data to buffer until sentinel is found,
                             then strip off the message, leaving the remainder
                             in the buffer.
                          '''
                          while not '
                  ' in self.buffer:
                              data = self.sock.recv(4096)
                              if not data:
                                  return ''
                              self.buffer += data
                          sentinel = self.buffer.index('
                  ') + 1
                          msg,self.buffer = self.buffer[:sentinel],self.buffer[sentinel:]
                          return msg
                  
                      def close(self):
                          self.sock.close()
                  
                  if __name__ == '__main__':
                      c = Client()
                      c.connect((HOST,PORT))
                      while True:
                          msg = c.get_msg()
                          if not msg:
                              break
                          print repr(msg)
                      c.close()
                  

                  这篇关于多个写入作为单个处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    1. <small id='HLT4a'></small><noframes id='HLT4a'>

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

                        <tbody id='HLT4a'></tbody>

                            <bdo id='HLT4a'></bdo><ul id='HLT4a'></ul>