forked from eshaan7/Flask-Shell2HTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
50 lines (41 loc) · 1.24 KB
/
basic.py
File metadata and controls
50 lines (41 loc) · 1.24 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
# system imports
import logging
import sys
# web imports
from flask import Flask
from flask_executor import Executor
from flask_shell2http import Shell2HTTP
# Flask application instance
app = Flask(__name__)
# Logging
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("flask_shell2http")
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# application factory
executor = Executor(app)
shell2http = Shell2HTTP(app, executor, base_url_prefix="/cmd/")
ENDPOINT_AND_CMD = "echo"
shell2http.register_command(endpoint=ENDPOINT_AND_CMD, command_name=ENDPOINT_AND_CMD)
# Test Runner
if __name__ == "__main__":
app.testing = True
c = app.test_client()
"""
The final executed command becomes:
```bash
$ echo hello world
```
"""
# make new request for a command with arguments
uri = f"/cmd/{ENDPOINT_AND_CMD}"
# timeout in seconds, default value is 3600
data = {"args": ["hello", "world"], "timeout": 60}
resp1 = c.post(uri, json=data).get_json()
print(resp1)
# fetch result
result_url = resp1["result_url"]
resp2 = c.get(result_url).get_json()
print(resp2)