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'\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简单传输数据的更多相关文章
- socket --自己简单的理解
一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可 ...
- socket.io简单入门(一.实现简单的图表推送)
引子:随着nodejs蓬勃发展,虽然主要业务系统因为架构健壮性不会选择nodejs座位应用服务器.但是大量的内部系统却可以使用nodejs试水,大量的前端开发人员转入全堆开发也是一个因素. 研究本例主 ...
- socket.io简单说明及在线抽奖demo
socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...
- 运用socket实现简单的服务器客户端交互
Socket解释: 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意 ...
- java Socket实现简单在线聊天(二)
接<java Socket实现简单在线聊天(一)>,在单客户端连接的基础上,这里第二步需要实现多客户端的连接,也就需要使用到线程.每当有一个新的客户端连接上来,服务端便需要新启动一个线程进 ...
- java Socket实现简单在线聊天(一)
最近的项目有一个在线网页交流的需求,由于很久以前做过的demo已经忘记的差不多了,因此便重新学习一下. 我计划的大致实现步骤分这样几大步: 1.使用awt组件和socket实现简单的单客户端向服务端持 ...
- 运用socket实现简单的ssh功能
在python socket知识点中已经对socket进行了初步的了解,那现在就使用这些知识来实现一个简单的ssh(Secure Shell)功能. 首先同样是建立两个端(服务器端和客户端) 需求是: ...
- 用Socket来简单实现IIS服务器
刚刚接触ASP.NET编程,为了更好的屡清楚服务器的处理过程,就用Socket模拟服务器来处理请求.用Socket来模拟服务器的时候,同样是自己来封装一些对应的类文件.包括 HttpRequest.H ...
- python socket 编程简单入门
想讲讲套接字的概念 套接字,即英文socket的中文意译,起源于20世纪70年代,是加利福利亚大学的伯克利版本UNIX(称为BSD UNIX)的一部分.目的是实现主机上运行的一个程序与另一个运行的程序 ...
随机推荐
- GNU的strong symbol和weak symbol
首先,同样的原型的两个函数在连个不同的c文件中都有定义,把这两个c文件编译.连接在一起,也没有什么错误.原因就是因为,gcc中有一个strong symbol和weak symbol的概念.默认函数定 ...
- 通过GUID生成可持久化的PID
byte[] buffer = Guid.NewGuid().ToByteArray(); ); GUID是微软针对UUID的实现,直接生成会大于long类型的最大长度. 但只要转换一下即可
- ISE联合modelsim功能仿真和综合后仿真
1.代码输入 (1).新建一个ISE工程,名字为count4. (2).新建一个verilog文件 (3).选择verilog module 输入file name为count4,单击next默认知道 ...
- 每日英语:The Biggest Distraction In The Office Is Sitting Next To You
The big push in office design is forcing co-workers to interact more. Cubicle walls are lower, offic ...
- NSIS安装程序制作工具判断系统是否安装.NET
前段时间忙了很久的系统总算上线了,由于是WinForm程序不能整个文件夹的发给客户使用.所以必须要打包,记得以前在VS2005中是自带部署功能的.现在换了VS2013那个部署功能完全弄不清方向.最后在 ...
- electron 的窗口设置最大化 最小化
/** * Created by Administrator on 2016/11/23. * 页面对窗口的一些操作封装,用于渲染进程 */ "use strict"; const ...
- stock article
stock 征服星辰大海 http://www.tianya.cn/95789158 论坛_悟道股市_天涯社区 http://www.tianya.cn/108318593/bbs?t=post 当下 ...
- 续写上一篇的数组or指针操作
C语言,同样使用if else while 这样的语法,但不同的人,就是有不同的实现方式,甚至是技巧. eg: #include <stdio.h> #include<string. ...
- 使用matplot做图--sin图像
# _*_ coding:utf-8 _*_ import numpy as np import matplotlib.pyplot as plt x = np.arange(-5, 5, 0.1) ...
- JAVA-MyEclipse第一个实例
相关资料: <21天学通Java Web开发> 实例代码: MyEclipse第一个实例1.打开MyEclipse程序.2.在PacKage视图->右击->New|Web Pr ...