-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasthook_server_ast.py
More file actions
75 lines (65 loc) · 2.1 KB
/
asthook_server_ast.py
File metadata and controls
75 lines (65 loc) · 2.1 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
from multiprocessing.connection import Listener
import pickle
from asthook import DIR
from asthook.static.ast import ast, Register
from asthook.utils import *
from asthook.static.module import ModuleStatic
import _thread
import sys
CALL = []
def proc(conn, index):
while True:
msg = conn.recv()
if msg == 'args':
msg = conn.recv()
args = pickle.loads(msg)
#print(args)
basepath = '%s/%s/' % (
DIR,
args.app.split('/')[-1])
CALL[index]["args"] = args
CALL[index]["basepath"] = basepath
conn.send("args OK")
elif msg == 'output':
msg = conn.recv()
Output.replace(pickle.loads(msg), index)
#print(Output.get_store(index))
conn.send("output OK")
elif msg == 'run':
Register.set_instance(index)
app = CALL[index]["args"].app
basepath = CALL[index]["basepath"]
args = CALL[index]["args"]
ModuleStatic(app, "%s" % (basepath), args)
ast(basepath, app, args, index, conn)
Output.print_static_module(index)
conn.send(Output.none_print(index))
elif msg == 'close':
conn.close()
break
def main(server, port):
global CALL
Output.init()
address = (server, port) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey=b'madkey')
while True:
conn = listener.accept()
CALL.append({})
index = len(CALL) - 1
print(f'connection accepted from {listener.last_accepted}')
_thread.start_new_thread(proc, (conn, index,))
listener.close()
if __name__ == '__main__':
print(sys.argv)
if len(sys.argv) < 3:
print(f"usage: {sys.argv[0]} <ip> <port>")
sys.exit(1)
main(sys.argv[1], int(sys.argv[2]))
#from multiprocessing.connection import Client
#
#address = ('localhost', 6000)
#conn = Client(address, authkey='secret password')
#conn.send('close')
## can also send arbitrary objects:
## conn.send(['a', 2.5, None, int, sum])
#conn.close()