struck
import struct
#b'\x0c\x00\x00\x00'
print(struct.pack('i',12))
s = struct.pack('i',12)
print(struct.unpack('i',s)[0])
复制代码
socket_server_tcp
from socket import *
import subprocess
import struct
# ip_port = ('127.0.0.1',8080)
# back_log=5
# buffer_size = 1024
#
# tcp_server = socket(AF_INET,SOCK_STREAM)
# tcp_server.bind(ip_port)
# tcp_server.listen(back_log)
#
# while True:
# conn,addr = tcp_server.accept()
# print('新的客户端链接',addr)
# while True:
# try:
# cmd=conn.recv(buffer_size)
# print('收到的客户端的命令',cmd)
#
# res = subprocess.Popen(cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
# err = res.stderr.read()
# if err:
# cmd_res = err
# else:
# cmd_res = res.stdout.read()
#
# if not cmd_res:
# cmd_res = '执行成功'.encode('gbk')
# #计算数据长度
# length =len(cmd_res)
# data_length=str
# #将数据长度发送给客户端
# conn.send(str(length).encode('utf-8'))
# #服务端接收到客户端发送的准备指令
# client_ready = conn.recv(buffer_size)
# #真正的进行发送数据
# if client_ready == b'ready':
# conn.send(cmd_res)
# except Exception as e:
# print(e)
# break
#------------------------------------------------------
ip_port = ('127.0.0.1',8080)
back_log=5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while True:
conn,addr = tcp_server.accept()
print('新的客户端链接',addr)
while True:
try:
cmd=conn.recv(buffer_size)
print('收到的客户端的命令',cmd)
res = subprocess.Popen(cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
err = res.stderr.read()
if err:
cmd_res = err
else:
cmd_res = res.stdout.read()
if not cmd_res:
cmd_res = '执行成功'.encode('gbk')
#计算数据长度
length =len(cmd_res)
data_length=struct.pack('i',length)
#此时已知第一次发送的长度是4个字节
conn.send(data_length)
conn.send(cmd_res)
except Exception as e:
print(e)
break
复制代码
socket_client_tcp
from socket import *
import struct
from functools import partial
# ip_port = ('127.0.0.1',8080)
# back_log=5
# buffer_size=1024
#
# tcp_client = socket(AF_INET,SOCK_STREAM)
# tcp_client.connect(ip_port)
#
# while True:
# cmd = input('>>:').strip()
# if not cmd:continue
# if cmd == 'quit':break
# tcp_client.send(cmd.encode('utf-8'))
# #解决粘包
# #客户端接收到服务端发送的数据长度
# length = tcp_client.recv(buffer_size)
# #客户端发送一个自定义的准备操作
# tcp_client.send(b'ready')
# length = int(length.decode('utf-8'))
#
# recv_size = 0
# recv_msg =b''
# while recv_size <length:
# recv_msg += tcp_client.recv(buffer_size)
# recv_size=len(recv_msg)
#
# print('命令的执行结果是',recv_msg.decode('gbk'))
# tcp_client.close()
#-----------------------------------------------------------
ip_port = ('127.0.0.1',8080)
back_log=5
buffer_size=1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
cmd = input('>>:').strip()
if not cmd:continue
if cmd == 'quit':break
tcp_client.send(cmd.encode('utf-8'))
#解决粘包
length_data = tcp_client.recv(4)
length = struct.unpack('i',length_data)[0]
# recv_size = 0
# recv_msg = b''
# while recv_size <length:
# recv_msg += tcp_client.recv(buffer_size)
# recv_size=len(recv_msg)
recv_msg = ''.join(iter(partial(tcp_client.recv,buffer_size),b''))
print('命令的执行结果是',recv_msg.decode('gbk'))
tcp_client.close()
复制代码
iter补充
l = ['a','b','c','d']
def test():
return l.pop()
x = iter(test,'b')
print(x.__next__())
print(x.__next__())
print(x.__next__())
from functools import partial
def add(x,y):
return x+y
print(add(1,2))
func = partial(add,1)
print(func(1))
print(func(2))
复制代码