-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipe.py
More file actions
111 lines (88 loc) · 3.45 KB
/
Copy pathpipe.py
File metadata and controls
111 lines (88 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding:utf-8 -*-
import subprocess
import sys
import threading
class LoopException(Exception):
"""循环异常自定义异常,此异常并不代表循环每一次都是非正常退出的"""
def __init__(self, msg="LoopException"):
self._msg = msg
def __str__(self):
return self._msg
class SwPipe():
"""
与任意子进程通信管道类,可以进行管道交互通信
"""
def __init__(self, commande, func, exitfunc, readyfunc=None,
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, code="GBK"):
"""
commande 命令
func 正确输出反馈函数
exitfunc 异常反馈函数
readyfunc 当管道创建完毕时调用
"""
self._thread = threading.Thread(target=self.__run, args=(commande, shell, stdin, stdout, stderr, readyfunc))
self._code = code
self._func = func
self._exitfunc = exitfunc
self._flag = False
self._CRFL = "\r\n"
def __run(self, commande, shell, stdin, stdout, stderr, readyfunc):
""" 私有函数 """
try:
self._process = subprocess.Popen(
commande,
shell=shell,
stdin=stdin,
stdout=stdout,
stderr=stderr
)
except OSError as e:
self._exitfunc(e)
fun = self._process.stdout.readline
self._flag = True
if readyfunc != None:
threading.Thread(target=readyfunc).start() # 准备就绪
while True:
line = fun()
if not line:
break
try:
tmp = line.decode(self._code)
except UnicodeDecodeError:
tmp = \
self._CRFL + "[PIPE_CODE_ERROR] <Code ERROR: UnicodeDecodeError>\n"
+ "[PIPE_CODE_ERROR] Now code is: " + self._code + self._CRFL
self._func(self, tmp)
self._flag = False
self._exitfunc(LoopException("While Loop break")) # 正常退出
def write(self, msg):
if self._flag:
# 请注意一下这里的换行
self._process.stdin.write((msg + self._CRFL).encode(self._code))
self._process.stdin.flush()
# sys.stdin.write(msg)#怎么说呢,无法直接用代码发送指令,只能默认的stdin
else:
raise LoopException("Shell pipe error from '_flag' not True!") # 还未准备好就退出
def start(self):
""" 开始线程 """
self._thread.start()
def destroy(self):
""" 停止并销毁自身 """
process.stdout.close()
self._thread.stop()
del self
if __name__ == '__main__': # 那么我们来开始使用它吧
e = None
# 反馈函数
def event(cls, line): # 输出反馈函数
sys.stdout.write(line)
def exit(msg): # 退出反馈函数
print(msg)
def ready(): # 线程就绪反馈函数
# e.write("adb forward tcp:41415 tcp:41415")
# e.write("drozer console connect --server 127.0.0.1:41415") # 执行
e.write("run app.package.list")
# e.write("echo Hello!World 你好中国!你好世界!")
# e.write("exit")
e = SwPipe("drozer console connect --server 127.0.0.1:41415", event, exit, ready)
e.start()