subprocess、struct模块的简单应用与ssh模型(黏包)
一、subprocess模块 #可以通过传递字符串命令,帮你去实现一些操作系统的命令。
import subprocess
res = subprocess.Popen("dir",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
ret = res.stdout.read().decode("gbk")
print(ret)
二、struct模块
import struct
res = struct.pack("i",6465789) #"q",8个字节
print(res) #b'\xfd\xa8b\x00'
print(len(res)) #
ret = struct.unpack("i",res)
print(ret) #(6465789,)
print(ret[0]) #
三、ssh模型(黏包)
server
import subprocess
import socket
import struct server = socket.socket()
server.bind(("127.0.0.1",8101))
server.listen(5)
while 1:
conn,addr = server.accept()
while 1:
try:
server_cmd = conn.recv(1024).decode("utf8")
res = subprocess.Popen(server_cmd,
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
if server_cmd == b"exit":
break
response1 = res.stdout.read()
response2 = res.stderr.read()
if response2:
pack = struct.pack("i",(len(response2)))
conn.send(pack)
conn.send(response2)
else:
pack = struct.pack("i",(len(response1)))
conn.send(pack)
conn.send(response1)
except Exception as e:
break
conn.close()
client
import socket
import struct
client = socket.socket()
client.connect(("127.0.0.1",8101))
while 1:
client_cmd = input("请输入您的命令:")
client.send(client_cmd.encode("utf8"))
if client_cmd == "exit":
break
if client_cmd == "":
continue
pack = client.recv(4)
print(pack)
data_length = struct.unpack("i",pack)[0]
print(data_length)
recv_data_length = 0
recv_data = b"" while recv_data_length < data_length:
data = client.recv(1024)
recv_data_length += len(data)
recv_data += data print(recv_data.decode("gbk")) client.close()
subprocess、struct模块的简单应用与ssh模型(黏包)的更多相关文章
- 模拟ssh、黏包、hashlib模块(MD5)
待补充..... 一.模拟ssh 二.黏包 1.黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cm ...
- 模拟ssh、黏包、hashlib模块
一.模拟ssh 1.subprocess模块 ipconfig -all dir subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system ...
- 记录:tf.saved_model 模块的简单使用(TensorFlow 模型存储与恢复)
虽然说 TensorFlow 2.0 即将问世,但是有一些模块的内容却是不大变化的.其中就有 tf.saved_model 模块,主要用于模型的存储和恢复.为了防止学习记录文件丢失或者蠢笨的脑子直接遗 ...
- 缓冲区 subprocess 黏包 黏包的解决方案
缓冲区: 将程序和网络解耦输入缓冲区输出缓冲区 print('>>>>', server.getsockopt(SOL_SOCKET, SO_SNDBUF)) 查看输出缓冲区大 ...
- day30 python学习 struct模块和 subprocess 模块
import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...
- (day27)subprocess模块+粘包问题+struct模块+ UDP协议+socketserver
目录 昨日回顾 软件开发架构 C/S架构 B/S架构 网络编程 互联网协议 socket套接字 今日内容 一.subprocess模块 二.粘包问题 三.struct模块 四.UDP 五.QQ聊天室 ...
- day 28 黏包 ssh模块 subprocess模块
套接字的信仰 一切皆文件 昨日作业: import socket sock=socket.socket() # TCP协议 IP_PORT=("127.0.0.1",8899) s ...
- 网络编程 --- subprocess模块,struct模块,粘包,UDP协议,socket_server模块
目录 subprocess模块 struct模块 粘包 UDP协议 socket_server模块 subprocess模块 作用: 1.可以帮你通过代码执行操作系统的终端命令 2.并返回终端执行命令 ...
- python笔记8 socket(TCP) subprocess模块 粘包现象 struct模块 基于UDP的套接字协议
socket 基于tcp协议socket 服务端 import socket phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 买 ...
随机推荐
- Selenium2+python自动化2.7-火狐44版本环境搭建(转)
转载地址:https://www.cnblogs.com/yoyoketang/p/selenium.html 前言 目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以 ...
- mysql存储过程demo
#删除存储过程 -- drop procedure if exists add_test; CREATE PROCEDURE add_test() begin #定义变量 declare client ...
- 高级UI-FloatingActionButton
FloatingActionButton为悬浮按钮,就是常见的那种悬浮在控件上,可以调出其他菜单的按钮 FloatingActionButton的特有属性 app:backgroundTint 按钮的 ...
- Django 之上下文处理器和中间件
一.上下文处理器 上下文处理器是可以返回一些数据,在全局模板中都可以使用.比如登录后的用户信息,在很多页面中都需要使用,那么我们可以放在上下文处理器中,就没有必要在每个视图函数中都返回这个对象. 在s ...
- Keras.NET
[翻译]Keras.NET简介 - 高级神经网络API in C# Keras.NET是一个高级神经网络API,它使用C#编写,并带有Python绑定,可以在Tensorflow.CNTK或The ...
- mysql子查询用法
mysql子查询用法 1 可以当值来用<pre>select id from hcyuyin_share where id=(select id from hcyuyin_share li ...
- Eclipse使用JDBC方式连接SQLServer2017
这篇博客写的比较详细了,图文并茂: https://blog.csdn.net/rebekahq/article/details/78691343 这里补充一些可能会遇到的问题: 1.与博客中不同之处 ...
- [NPM错误]npm ERR! Unexpected end of JSON input while parsing near ‘’
[错误描述] npm ERR! Unexpected end of JSON input while parsing near ‘ ’ [前提描述] 在安装vue2-editor时,中断暂停了,再次 ...
- 转录组组装软件stringtie
StringTie是約翰·霍普金斯大學计算机生物中心开发的一款转录组组装软件,在组装转录本的完整度,精度和速度方面都较以往的cufflinks 有很大的提升,也是目前有参考基因组转录组主流的组装软件. ...
- Nginx里的root/index/alias/proxy_pass的意思
1.[alias] 别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向[alias]配置的路径.如: location /test/ { alias /home/sftp/i ...