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

socket client简单传输数据的更多相关文章

  1. socket --自己简单的理解

    一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可 ...

  2. socket.io简单入门(一.实现简单的图表推送)

    引子:随着nodejs蓬勃发展,虽然主要业务系统因为架构健壮性不会选择nodejs座位应用服务器.但是大量的内部系统却可以使用nodejs试水,大量的前端开发人员转入全堆开发也是一个因素. 研究本例主 ...

  3. socket.io简单说明及在线抽奖demo

    socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...

  4. 运用socket实现简单的服务器客户端交互

    Socket解释: 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意 ...

  5. java Socket实现简单在线聊天(二)

    接<java Socket实现简单在线聊天(一)>,在单客户端连接的基础上,这里第二步需要实现多客户端的连接,也就需要使用到线程.每当有一个新的客户端连接上来,服务端便需要新启动一个线程进 ...

  6. java Socket实现简单在线聊天(一)

    最近的项目有一个在线网页交流的需求,由于很久以前做过的demo已经忘记的差不多了,因此便重新学习一下. 我计划的大致实现步骤分这样几大步: 1.使用awt组件和socket实现简单的单客户端向服务端持 ...

  7. 运用socket实现简单的ssh功能

    在python socket知识点中已经对socket进行了初步的了解,那现在就使用这些知识来实现一个简单的ssh(Secure Shell)功能. 首先同样是建立两个端(服务器端和客户端) 需求是: ...

  8. 用Socket来简单实现IIS服务器

    刚刚接触ASP.NET编程,为了更好的屡清楚服务器的处理过程,就用Socket模拟服务器来处理请求.用Socket来模拟服务器的时候,同样是自己来封装一些对应的类文件.包括 HttpRequest.H ...

  9. python socket 编程简单入门

    想讲讲套接字的概念 套接字,即英文socket的中文意译,起源于20世纪70年代,是加利福利亚大学的伯克利版本UNIX(称为BSD UNIX)的一部分.目的是实现主机上运行的一个程序与另一个运行的程序 ...

随机推荐

  1. c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题

    c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...

  2. unity, Find References In Scene

    材质,脚本,shader等都可以通过Find References In Scene查看引用情况,如图. 当对一个文件点击Find References In Scene后,搜索命令会显示到Scene ...

  3. 使用.net的跟踪诊断来记录wcf消息

    首先在项目的config文件中定义以下结点: <system.diagnostics> <sources> <source name="System.Servi ...

  4. [na]icmp重定向

    这个东西最多平时翻看书时候yy以下或者gns3模拟一下, 实际中还真不曾遇到,直到今天,帮别人解决一个问题时候,抓icmp包发现这个.....忘记了原理,梳理一下 icmp重定向问题 参考

  5. 【Android】自己定义ListView的Adapter报空指针异常解决方法

    刚刚使用ViewHolder的方法拉取ListView的数据,可是总会报异常. 细致查看代码.都正确. 后来打开adapter类,发现getView的返回值为null. 即return null. 将 ...

  6. 手机防盗之获取手机经纬度(Android)

    获取手机经纬度有gps , network , 基站 三种方式,我们可以根据定位的条件,获取一个最好的定位方式.然后将获取到经纬度信息发送到指定的手机号码中. /* * 单态只允许存在一个实例. * ...

  7. python常用的十进制、16进制、字符串、字节串之间的转换

    进行协议解析时,总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上,直接上例子 整数之间的进制转换: 10进制转16进制: hex(16)  ==>  0x10 ...

  8. 索引长度过长 ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

    1.发现问题 今天在修改innodb表的某个列的长度时,报如下错误: alter table test2 modify column id varchar(500); ERROR 1071 (4200 ...

  9. nrm 的使用说明

    nrm -- NPM registry 管理工具 开发的npm registry 管理工具 nrm, 能够查看和切换当前使用的registry, 最近NPM经常 down 掉, 这个还是很有用的哈哈 ...

  10. Log4j常用配置及使用

    Log4j常用配置及使用 2016-12-14 目录 1 添加log4j依赖2 使用代码配置并调用log  2.1 代码配置LogConfiguration.java  2.2 调用  2.3 结果3 ...