socket client简单传输数据
2015-08-11 09:53
218 查看
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'\x04\x00' >>> (1024).to_bytes(10, byteorder='big') b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00' >>> (-1024).to_bytes(10, byteorder='big', signed=True) b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00' >>> x = 1000 >>> x.to_bytes((x.bit_length() // 8) + 1, byteorder='little') b'\xe8\x03' 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'\x00\x10', byteorder='big') 16 >>> int.from_bytes(b'\x00\x10', byteorder='little') 4096 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) -1024 >>> int.from_bytes(b'\xfc\x00', 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
相关文章推荐
- php -> =>的问题
- TCP/IP协议学习之一(以太网帧格式)
- 【SpringMVC】annotation配置(事务不失效)
- configure配置及其cmake配置-静态库生成
- Spring小记(一)
- webservice和restful的区别
- 再也不会分不清楚offsetheight clientheight scrollheight offsettop clienttop scrollheight
- 线程面试题
- C# 对象、文件与二进制串(byte数组)之间的转换
- 操作系统——页面置换算法详解
- hdoj 1232畅通工程
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- SimpleDateFormat时间格式问题
- 互联网金融网络借贷系统架构
- 利用正则表达式转换String 为int或Double
- hdu 3572 Task Schedule (dinic算法)
- Maven使用笔记(二)Maven的工作原理
- 使用超酷的jQuery缩略图生成插件NailThumb制作漂亮的缩略图web应用
- asdfasdf
- Java内存状况查看方法和分析工具