socket client简单传输数据

简介:   1.整数转换为用于TCP传输的二进制 _host = "127.0.0.1" _port = 5678 _address = (_host, _port) s=socket.

 

 

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

 

相关文章
|
4月前
|
存储 Python
Python网络编程基础(Socket编程) UDP 发送和接收数据
【4月更文挑战第10天】对于UDP客户端而言,发送数据是一个相对简单的过程。首先,你需要构建一个要发送的数据报,这通常是一个字节串(bytes)。然后,你可以调用socket对象的`sendto`方法,将数据报发送到指定的服务器地址和端口。
|
4月前
|
网络协议 Python
python中socket客户端发送和接收数据
【4月更文挑战第7天】本教程聚焦TCP客户端数据发送与接收。使用Python的`socket`模块,通过`send()`发送字节串至服务器,如`client_socket.send(message_bytes)`;用`recv()`接收数据,如`received_data = client_socket.recv(buffer_size)`。异常处理确保网络错误时程序健壮性,例如`try-except`捕获`socket.error`。理解和掌握这些基础操作对于构建稳定的TCP客户端至关重要。
成功解决: Client network socket disconnected before secure TLS connection was established
这篇文章记录了在使用Avue时遇到的"Client network socket disconnected before secure TLS connection was established"错误的解决方法,即通过修改为国内镜像(如淘宝npm镜像)来解决安装问题,并提供了具体的命令示例以及安装成功后的截图。
成功解决: Client network socket disconnected before secure TLS connection was established
|
1月前
|
网络协议 数据格式 Python
python Socket无限发送接收数据方式
Socket是指套接字,是对网络中不同主机上的应用进程之间进行双向通信的端点的一种抽象。 一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制。
|
3月前
|
网络协议
逆向学习网络篇:通过Socket建立连接并传输数据
逆向学习网络篇:通过Socket建立连接并传输数据
42 0
|
3月前
|
监控 网络协议 Java
Java Socket编程 - 基于TCP方式的二进制文件传输
Java Socket编程 - 基于TCP方式的二进制文件传输
37 0
|
4月前
|
网络协议 Java 网络安全
【计算机网络】—— Socket通信编程与传输协议分析
【计算机网络】—— Socket通信编程与传输协议分析
|
4月前
|
存储 Python
Python网络编程基础(Socket编程)接收和发送数据
【4月更文挑战第9天】在UDP服务器编程中,我们已经创建了一个UDP套接字并绑定了地址和端口。接下来,服务器需要能够接收来自客户端的数据,并能够对这些数据进行处理和响应。下面,我们将详细讲解如何在UDP服务器中接收和发送数据。
|
4月前
|
流计算 Windows
【极数系列】Flink集成DataSource读取Socket请求数据(09)
【极数系列】Flink集成DataSource读取Socket请求数据(09)
110 3
|
4月前
|
Oracle 关系型数据库 Java
从 Oracle 数据库的 socket 中读取数据时,没有更多的数据可供读取。这可能是由于以下原因导致的:
【1月更文挑战第26天】【1月更文挑战第125篇】从 Oracle 数据库的 socket 中读取数据时,没有更多的数据可供读取。这可能是由于以下原因导致的:
37 1