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. [svc][op]磁盘MBR分区机制- inode/Block深入实战

    一 思路: 1,磁盘物理结构及大小计算 2,分区 MBR GPT知识 3,fdisk分区 挂载 自动挂载 4,格式化文件系统 5,inode block 6,软硬链接 查看磁盘: [root@moba ...

  2. JNI中GetStringChars函数中的isCopy

    一直不明白这个isCopy是什么意思,只知道每次使用的时候都传NULL,今天看到了相关的资料,特来分享下. 当从JNI函数GetStringChars中返回得到字符串B时,如果B是原始字符串java. ...

  3. Java NIO- Selector 使用示例

    Selector selector = Selector.open(); channel.configureBlocking(false); SelectionKey key = channel.re ...

  4. nyoj138 哈希的简单应用(查找)

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...

  5. ftp实现文件上传(下载)

    例子代码 package getUrlPic; import java.io.ByteArrayInputStream; import java.io.IOException; import java ...

  6. perl ExtUtils::Manifest

    Can't locate ExtUtils/Manifest.pm in @INC (you may need to install the ExtUtils::Manifest module) yu ...

  7. 一款基于jquery滑动后固定于顶部的导航

    之前已为大家介绍了好多css3实现的导航菜单.今天分享一款基于jquery滑动后固定于顶部的导航.这款导航的特点是初始位于顶部下面一百个像素,当鼠标滚动时到下方,导航一直处于顶部.效果图如下: 在线预 ...

  8. Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=default}) (state=08S01,code=0)

    sparksql 2.和hive2.1.1 由于sparksql中的hive-cli 等包的版本是1.2的需要自己下载,下载替换之后不报错,替换之前做好备份

  9. 【Visual Studio】解决方案未保存,请先保存你的解决方案,然后再管理Nuget包

    上网下的Demo,文件夹中没有.sln文件,用VS打开.csproj文件来打开方案.此时可能因为一些引用问题想打开Nuget包管理器,会弹出如下提示: 解决方案未保存,请先保存你的解决方案,然后再管理 ...

  10. 【WPF/C#】使用BackgroundWorker实现多线程/异步操作

    做WPF时需要做一个异步加载时的Loading遮罩,搜Stackoverflow看到很多方法,看到了这个插件: BusyIndicator in the extended WPF Toolkit 同时 ...