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)的一部分.目的是实现主机上运行的一个程序与另一个运行的程序 ...
随机推荐
- Generalized Linear Models
作者:桂. 时间:2017-05-22 15:28:43 链接:http://www.cnblogs.com/xingshansi/p/6890048.html 前言 主要记录python工具包:s ...
- How To Setup Apache Hadoop On CentOS
he Apache Hadoop software library is a framework that allows for the distributed processing of large ...
- 限制 nuget 更新包的版本号
今天在搜索其它问题的时候,突然发现一个使用 nuget 的小技巧. 因为浏览器兼容性的问题,很多网站项目引用的 jQuery 组件版本需要保持在 2.0 以下,因为 2.0 以上需要现代浏览器的支持, ...
- [Jobdu] 题目1391:顺时针打印矩阵
题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2 ...
- vim-snipmate的c.snippets(2016.7.10)
## Main # main snippet main int main ( void ) { ${} ; } ##include snippet inc #include <${:stdio} ...
- Probability Concepts
Probability Concepts Unconditional probability and Conditional Probability Unconditional Probability ...
- paip.分布式应用系统java c#.net php的建设方案
paip.分布式应用系统java c#.net php的建设方案 1. 基础设施的建立 1 2. 本地的的调用API 1 3. 数据的传输 1 4. 代码的传输 1 5. 代码的自动热编译 2 6. ...
- JVM building
http://hg.openjdk.java.net/jdk10/jdk10/raw-file/tip/README file:///D:/JDK/jdk11/jdk/doc/building.htm ...
- canvas.drawBitmap(bitmap, src, dst, paint)
// GameView.drawImage(canvas, mBitDestTop, miDTX, mBitQQ.getHeight(), mBitDestTop.getWidth(), mBitDe ...
- Eclipse+tomcat+axis2进行web service部署
用Eclipse+axis2+tomcat进行web service部署 2016-12-07 目录 1 安装JDK 1.1 下载JDK 1.2 安装和配置JDK 1.3 验证2 安装Ecli ...