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)的一部分.目的是实现主机上运行的一个程序与另一个运行的程序 ...
随机推荐
- MATLAB(2)——小波工具箱使用简介
作者:桂. 时间:2017-02-19 21:47:27 链接:http://www.cnblogs.com/xingshansi/articles/6417638.html 前言 本文主要介绍MA ...
- hadoop(2.x)以hadoop2.2为例完全分布式最新高可靠安装文档
问题导读:1.如何配置各个节点之间无密码互通?2.启动hadoop,看不到进程的原因是什么?3.配置hadoop的步骤是什么? 4.有哪些配置文件需要修改?5.如果没有配置文件,该如何找到该配置文件? ...
- HDU 2444 The Accomodation of Students二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
- 黑马day18 鼠标事件&图片变大
有时候我们在淘宝网或者京东商城上浏览要购买的商品的时候当把鼠标移动到图图片上的时候会发现图片放大.然后鼠标移动,图片也会跟着移动,接下来我就使用jquery来实现这样的效果: 这是图片文件夹: < ...
- string.format的用途联想
还有IFormattable,感觉这辈子都用不到.. 设想了一个物品简介界面 分别是Title,分割线,内容. "{0:Red} \n {1:S1} \n 简介 \n {2:Green}&q ...
- 【Android】9.3 自定义列表视图的外观
分类:C#.Android.VS2015: 创建日期:2016-02-18 一.简介 自定义的列表视图通常用Resources/Layout文件夹下的axml文件中的资源来声明,适配器则通过Id去加载 ...
- 中间件监控之Apache
补 系统架构 nginx接到请求后把请求转发到tomcat,还有种方式是转发到apache(php项目),或者其他语言的应用服务器(放置我们的项目) ngnix:是web服务器,接受和转发请求用的,不 ...
- hdoj1069 Monkey and Banana
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Hystrix使用说明,配置参数说明
一.什么情况下会触发fallback方法? 名字 描述 触发fallback EMIT 值传递 NO SUCCESS 执行完成,没有错误 NO FAILURE 执行抛出异常 YES TIMEOUT 执 ...
- Python以不可见字符作为列分割符
# -*- coding: utf-8 -*- import sys import time CTRL_A='\x01' CTRL_B='\x02' thedate = '' thetime = '' ...