zoukankan      html  css  js  c++  java
  • Sending Binary Data Python Module of the Week

    Sending Binary Data - Python Module of the Week

    Sending Binary Data

    Sockets transmit streams of bytes. Those bytes can contain text messages, as in the previous examples, or they can be made up of binary data that has been encoded for transmission. To prepare binary data values for transmission, pack them into a buffer with struct.

    This client program encodes an integer, a string of two characters, and a floating point value into a sequence of bytes that can be passed to the socket for transmission.

    import binascii
    import socket
    import struct
    import sys
    
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    
    values = (1, 'ab', 2.7)
    packer = struct.Struct('I 2s f')
    packed_data = packer.pack(*values)
    
    try:
        
        # Send data
        print >>sys.stderr, 'sending "%s"' % binascii.hexlify(packed_data), values
        sock.sendall(packed_data)
    
    finally:
        print >>sys.stderr, 'closing socket'
        sock.close()
    

    The server program uses the same Struct specifier to unpack the bytes it receives.

    import binascii
    import socket
    import struct
    import sys
    
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.bind(server_address)
    sock.listen(1)
    
    unpacker = struct.Struct('I 2s f')
    
    while True:
        print >>sys.stderr, '\nwaiting for a connection'
        connection, client_address = sock.accept()
        try:
            data = connection.recv(unpacker.size)
            print >>sys.stderr, 'received "%s"' % binascii.hexlify(data)
    
            unpacked_data = unpacker.unpack(data)
            print >>sys.stderr, 'unpacked:', unpacked_data
            
        finally:
            connection.close()
    

    Running the client produces:

    $ python ./socket_binary_client.py
    
    sending "0100000061620000cdcc2c40" (1, 'ab', 2.7)
    closing socket

    And the server shows the values it receives:

    $ python ./socket_binary_server.py
    
    waiting for a connection
    received "0100000061620000cdcc2c40"
    unpacked: (1, 'ab', 2.700000047683716)
    
    waiting for a connection

    The floating point value loses some precision as it is packed and unpacked, but otherwise the data is transmitted as expected. One thing to keep in mind is that depending on the value of the integer, it may be more efficient to convert it to text and then transmit, instead of using struct. The integer 1 uses one byte when represented as a string, but four when packed into the structure.

  • 相关阅读:
    关于 语文物理 和 数学物理
    黎耀天 共量子 我拍了 几张照片 给你们 当 素材
    看了 陈记抄 同学 的 《随便聊聊之量子力学中的散射理论》
    极坐标系 下的 牛顿第二定律
    jQuery 工具类库集锦
    Javascript跨域问题解决方案
    js禁止从浏览器缓存读取消息
    关于双击事件.MouseEvent.DOUBLE_CLICK
    div的打开与关闭js
    利用通道完成文件的复制 (非直接缓存区)
  • 原文地址:https://www.cnblogs.com/lexus/p/2843238.html
Copyright © 2011-2022 走看看