socket client简单传输数据

1.整数转换为用于TCP传输的二进制
	_host = "127.0.0.1"
        _port = 5678
        _address = (_host, _port)
	
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        connect_result = s.connect(self._address)
	#二进制的字符串
        s.send(b'welcome to server!')
	# !代表的是大字节序
	s.send(struct.pack(">i",12345))
		
	#与erlang的不定长数据包,先接受报头。
	bytes_msg_length = s.recv(2)
		
        #解压数据,返回值为一个tuple,有效值为tuple内第一个位置。
        msg_length= struct.unpack(">h", bytes_msg_length)
		
        bytes_msg= s.recv(msg_length[0])        
        msg= struct.unpack(">f", bytes_msg)
	print(msg[0])

数据类型的转换:

一、整数和二进制数据之间的转换

(1243).to_bytes(4, byteorder='big')

出自Python3.4文档

int.to_bytes(length, byteorder, *, signed=False) 
Return an array of bytes representing an integer.

>>> (1024).to_bytes(2, byteorder='big')
b'x04x00'
>>> (1024).to_bytes(10, byteorder='big')
b'x00x00x00x00x00x00x00x00x04x00'
>>> (-1024).to_bytes(10, byteorder='big', signed=True)
b'xffxffxffxffxffxffxffxffxfcx00'
>>> x = 1000
>>> x.to_bytes((x.bit_length() // 8) + 1, byteorder='little')
b'xe8x03'
The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes.

The byteorder argument determines the byte order used to represent the integer. If byteorder is "big", the most significant byte is at the beginning of the byte array. If byteorder is "little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value.

The signed argument determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised. The default value for signed is False.

int.from_bytes((1243).to_bytes(4, byteorder='big'), byteorder='big')

int.from_bytes(bytes, byteorder, *, signed=False) 
Return the integer represented by the given array of bytes.

>>> int.from_bytes(b'x00x10', byteorder='big')
16
>>> int.from_bytes(b'x00x10', byteorder='little')
4096
>>> int.from_bytes(b'xfcx00', byteorder='big', signed=True)
-1024
>>> int.from_bytes(b'xfcx00', byteorder='big', signed=False)
64512
>>> int.from_bytes([255, 0, 0], byteorder='big')
16711680
The argument bytes must either be a bytes-like object or an iterable producing bytes.

The byteorder argument determines the byte order used to represent the integer. If byteorder is "big", the most significant byte is at the beginning of the byte array. If byteorder is "little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value.

The signed argument indicates whether two’s complement is used to represent the integer.

二进制的拼接:

aa = 123
dd = "汉字"
#bytesToInt是自定义的将int型转换为bytes的函数,参照上边的int.to_bytes() bb = intToBytes(aa) print("bb", bb) #二进制的拼接 cc = bb + bb+bb + bytes(dd, "utf8") print("cc", cc) print("len", len(cc)) #bytesToInt是自定义的将bytes转换为int型的函数,参照上边的int.from_bytes() print("---", bytesToInt(cc[1:4])) print("---", bytesToInt(cc[4:8])) print("---", bytesToInt(cc[8:12])) print("---", bytesToInt(cc[-10:-6])) print("---", (cc[-6:]).decode("utf8"))

方案二:

多个数据的合并

import struct
from ctypes import create_string_buffer  

#------------------------------------
#需要向一个数据包中多次压入数据
#-------------------------------------------

format_1 = ">i"
buffer_1 = struct.pack(format_1, 20)  

format_len_1 = struct.calcsize(format_1)
print(buffer_1)
print(format_len_1)

print("___________________________________")


buf = create_string_buffer(12)  
print("--", repr(buf.raw)) 
struct.pack_into(">iii", buf, 0, 1, 2, -1) 
print("--", repr(buf.raw)) 
print("--", buf.raw) 

print(struct.unpack_from(">iii", buf, 0)  ) 

#二进制的拼接
head = create_string_buffer(16)
body = create_string_buffer(16)
all = create_string_buffer(32) 
all.raw = head.raw + body.raw
原文地址:https://www.cnblogs.com/ribavnu/p/4720130.html