forked from ViperX7/Alpaca-Turbo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
interact.py
81 lines (66 loc) · 1.64 KB
/
interact.py
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
"""
Interactive library
"""
import os
from signal import SIGTERM
from pexpect.popen_spawn import PopenSpawn
class Process(PopenSpawn):
"""process"""
def __init__(
self,
cmd,
timeout=30,
maxread=2000,
searchwindowsize=None,
logfile=None,
cwd=None,
env=None,
encoding=None,
codec_errors="strict",
preexec_fn=None,
):
super().__init__(
cmd,
timeout,
maxread,
searchwindowsize,
logfile,
cwd,
env,
encoding,
codec_errors,
preexec_fn,
)
with open("./pid", "w", encoding="utf-8") as file:
file.writelines([str(self.pid)])
def kill(self, sig):
os.remove("pid")
return super().kill(sig)
def killx(self):
return self.kill(SIGTERM)
def recvuntil(self, data):
"""Recv data until a pattern is found"""
data = data if isinstance(data, bytes) else data.encode("utf-8")
info = b""
while data not in info:
info += self.read(1)
# print(info)
return info
def recv(self, *cfg):
data = self.read(*cfg)
# print(data)
return data
def interactive(self, *cfg):
return self.interact(*cfg)
def sendline(self, line):
msg = line + "\n"
self.send(msg)
def main():
prog = Process("cmd.exe")
prog.sendline("hi")
# o = prog.read(1)
# print(o)
# input()
# prog.recvuntil("..............")
prog.interact()
_ = main() if __name__ == "__main__" else None