forked from eshaan7/Flask-Shell2HTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_callback.py
40 lines (32 loc) · 1.11 KB
/
with_callback.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
# 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)
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="echo/callback", command_name="echo", 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("/echo/callback", json=data)
# request another new process
data = {"args": ["Hello", "Friend!"], "callback_context": {"testkey": "testvalue"}}
c.post("/echo/callback", json=data)