forked from eshaan7/Flask-Shell2HTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith_callback.py
More file actions
42 lines (33 loc) · 1.1 KB
/
with_callback.py
File metadata and controls
42 lines (33 loc) · 1.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
# web imports
from flask import Flask
from flask_executor import Executor
from flask_executor.futures import Future
from flask_shell2http import Shell2HTTP
# Flask application instance
app = Flask(__name__)
# application factory
executor = Executor(app)
shell2http = Shell2HTTP(app, executor, base_url_prefix="/cmd/")
ENDPOINT = "echo"
def my_callback_fn(extra_callback_context, future: Future):
"""
Will be invoked on every process completion
"""
print("[i] Process running ?:", future.running())
print("[i] Process completed ?:", future.done())
print("[+] Result: ", future.result())
# future.result() returns a dictionary
print("[+] Context: ", extra_callback_context)
shell2http.register_command(
endpoint=ENDPOINT, command_name=ENDPOINT, callback_fn=my_callback_fn
)
# Test Runner
if __name__ == "__main__":
app.testing = True
c = app.test_client()
# request new process
data = {"args": ["hello", "world"]}
c.post(f"cmd/{ENDPOINT}", json=data)
# request another new process
data = {"args": ["Hello", "Friend!"]}
c.post(f"cmd/{ENDPOINT}", json=data)