-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrabbitmq_worker.py
65 lines (46 loc) · 1.67 KB
/
rabbitmq_worker.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
import pika
import time
import subprocess
from pprint import pprint
import os
import sys
import shlex
# against connection closed exception of rabbitmq
for i in range(0, 1000):
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'
#heartbeat=3600,
#blocked_connection_timeout=3600
))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
# print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body.decode())
# arguments=body.decode().split()
arguments=shlex.split(body.decode())
# pprint(arguments)
process = subprocess.Popen(
["/home/seclab/emulator-launch-local.sh",
arguments[0]], # example with 1 arg to shell
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# executable="/usr/bin/bash",
out, err = process.communicate()
return_code = process.poll()
out = out.decode(sys.stdin.encoding)
err = err.decode(sys.stdin.encoding)
# print(out, err)
with open("x.stdout","a+") as fd_stdout:
fd_stdout.write(out)
with open("x.stderr","a+") as fd_stderr:
fd_stderr.write(err)
print(" [x] Done")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)
# channel.start_consuming()
try:
channel.start_consuming()
except Exception as e:
print(e)