Skip to content

Commit 12cbc22

Browse files
committed
update docs with more examples/use-cases, add json schema
1 parent 6c9ee3c commit 12cbc22

File tree

5 files changed

+68
-14
lines changed

5 files changed

+68
-14
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66
</a>
77
[![flask-shell2http on pypi](https://img.shields.io/pypi/v/flask-shell2http)](https://pypi.org/project/Flask-Shell2HTTP/)
88

9-
A minimalist [Flask](https://github.com/pallets/flask) extension that serves as a REST API wrapper for python's subprocess API.
9+
A minimalist [Flask](https://github.com/pallets/flask) extension that serves as a RESTful/HTTP wrapper for python's subprocess API.
1010

1111
- **Convert any command-line tool into a REST API service.**
12-
- Execute pre-defined shell commands asynchronously and securely via flask's endpoints.
12+
- Execute pre-defined shell commands asynchronously and securely via flask's endpoints with dynamic arguments, file upload, callback function capabilities.
1313
- Designed for binary to binary/HTTP communication, development, prototyping, remote control and [more](https://flask-shell2http.readthedocs.io/en/stable/Examples.html).
1414

15-
Inspired by the work of awesome folks over at [msoap/shell2http](https://github.com/msoap/shell2http).
1615

1716
## Use Cases
1817

1918
- Set a script that runs on a succesful POST request to an endpoint of your choice. See [Example code](examples/run_script.py).
2019
- Map a base command to an endpoint and pass dynamic arguments to it. See [Example code](examples/basic.py).
2120
- Can also process multiple uploaded files in one command. See [Example code](examples/multiple_files.py).
2221
- This is useful for internal docker-to-docker communications if you have different binaries distributed in micro-containers. See [real-life example](https://github.com/intelowlproject/IntelOwl/blob/develop/integrations/peframe/app.py).
23-
- You can define a callback function/ use signals to listen for process completion. See [Example code](examples/with_callback.py). Meybe want to intercept on completion and update the result ? See [Example code](examples/custom_save_fn.py)
24-
- Currently, all commands run asynchronously (default timeout is 3600 seconds), so result is not available directly. An option _may_ be provided for this in future release.
22+
- You can define a callback function/ use signals to listen for process completion. See [Example code](examples/with_callback.py).
23+
- Maybe want to pass some additional context to the callback function ?
24+
- Maybe intercept on completion and update the result ? See [Example code](examples/custom_save_fn.py)
25+
- Currently, all commands run asynchronously (default timeout is 3600 seconds), so result is not available directly. An option _may_ be provided for this in future releases for commands that return immediately.
2526

2627
> Note: This extension is primarily meant for executing long-running
2728
> shell commands/scripts (like nmap, code-analysis' tools) in background from an HTTP request and getting the result at a later time.
@@ -35,7 +36,8 @@ from the [documentation](https://flask-shell2http.readthedocs.io/) to get starte
3536

3637
I highly recommend the [Examples](https://flask-shell2http.readthedocs.io/en/stable/Examples.html) section.
3738

38-
## Why?
39+
## Inspiration
3940

4041
This was initially made to integrate various command-line tools easily with [Intel Owl](https://github.com/intelowlproject/IntelOwl), which I am working on as part of Google Summer of Code.
4142

43+
The name was inspired by the awesome folks over at [msoap/shell2http](https://github.com/msoap/shell2http).

docs/source/Examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ I have created some example python scripts to demonstrate various use-cases. The
77
- [multiple_files.py](https://github.com/Eshaan7/Flask-Shell2HTTP/blob/master/examples/multiple_files.py): Upload multiple files for a single command.
88
- [with_callback.py](https://github.com/Eshaan7/Flask-Shell2HTTP/blob/master/examples/with_callback.py): Define a callback function that executes on command/process completion.
99
- [with_signals.py](https://github.com/Eshaan7/Flask-Shell2HTTP/blob/master/examples/with_signals.py): Using [Flask Signals](https://flask.palletsprojects.com/en/1.1.x/signals/) as callback function.
10-
- [custom_save_fn.py](https://github.com/Eshaan7/Flask-Shell2HTTP/blob/master/examples/custom_save_fn.py): There may be cases where the process doesn't print result to standard output but to a file/database. This example shows how to intercept the future object after completion and update it's result attribute.
10+
- [custom_save_fn.py](https://github.com/Eshaan7/Flask-Shell2HTTP/blob/master/examples/custom_save_fn.py): There may be cases where the process doesn't print result to standard output but to a file/database. This example shows how to pass additional context to the callback function, intercept the future object after completion and update it's result attribute before it's ready to be consumed.

docs/source/Quickstart.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ app = Flask(__name__)
2727
executor = Executor(app)
2828
shell2http = Shell2HTTP(app=app, executor=executor, base_url_prefix="/commands/")
2929

30-
shell2http.register_command(endpoint="saythis", command_name="echo", callback_fn=None)
30+
def my_callback_fn(context, future):
31+
# additional user-defined callback function
32+
print(context, future.result())
33+
34+
shell2http.register_command(endpoint="saythis", command_name="echo", callback_fn=my_callback_fn)
3135
```
3236

3337
Run the application server with, `$ flask run -p 4000`.
@@ -53,11 +57,13 @@ print("Result:", resp.json())
5357

5458
</details>
5559

60+
> Note: You can see the JSON schema for the POST request [here](https://github.com/Eshaan7/Flask-Shell2HTTP/blob/master/post-request-schema.json).
61+
5662
returns JSON,
5763

5864
```json
5965
{
60-
"key": "ddbe0a94847c",
66+
"key": "ddbe0a94",
6167
"result_url": "http://localhost:4000/commands/saythis?key=ddbe0a94",
6268
"status": "running"
6369
}

docs/source/index.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ Welcome to Flask-Shell2HTTP!
55
.. image:: https://img.shields.io/lgtm/grade/python/g/Eshaan7/Flask-Shell2HTTP.svg?logo=lgtm&logoWidth=18
66
.. image:: https://img.shields.io/pypi/v/flask-shell2http
77

8-
A minimalist Flask_ extension that serves as a REST API wrapper for python's subprocess API.
8+
A minimalist Flask_ extension that serves as a RESTful/HTTP wrapper for python's subprocess API.
99

1010
- **Convert any command-line tool into a REST API service.**
1111
- Execute shell commands asynchronously and safely via flask's endpoints.
12-
13-
Inspired by the work of awesome folks over at shell2http_.
12+
- Designed for binary to binary/HTTP communication, development, prototyping, remote control and more.
1413

1514
.. _Flask: https://github.com/pallets/flask
16-
.. _shell2http: https://github.com/msoap/shell2http
1715

1816
**Use Cases:**
1917

2018
- Set a script that runs on a succesful POST request to an endpoint of your choice.
2119
- Map a base command to an endpoint and pass dynamic arguments to it.
2220
- Can also process multiple uploaded files in one command.
23-
- Currently, all commands are run asynchronously, so result is not available directly. An option would be provided for this in future release.
21+
- This is useful for internal docker-to-docker communications if you have different binaries distributed in micro-containers.
22+
- You can define a callback function/ use signals to listen for process completion.
23+
- Currently, all commands run asynchronously (default timeout is 3600 seconds), so result is not available directly. An option _may_ be provided for this in future release.
2424

2525
`Note: This extension is primarily meant for executing long-running
2626
shell commands/scripts (like nmap, code-analysis' tools) in background from an HTTP request and getting the result at a later time.`

post-request-schema.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"type": "object",
4+
"title": "The POST request schema",
5+
"default": {},
6+
"examples": [
7+
{
8+
"args": ["hello", "world"],
9+
"timeout": 60,
10+
"callback_context": {
11+
"read_result_from": "/path/to/saved/file",
12+
"force_success": true
13+
}
14+
}
15+
],
16+
"required": [],
17+
"properties": {
18+
"args": {
19+
"type": "array",
20+
"title": "Arguments that will be appended to the base command",
21+
"default": [],
22+
"examples": [
23+
["hello", "world"],
24+
["@inputfile", "@someotherfile"]
25+
]
26+
},
27+
"timeout": {
28+
"type": "integer",
29+
"title": "subprocess timeout in seconds",
30+
"description": "Maximum timeout after which subprocess fails if not already complete.",
31+
"default": 3600
32+
},
33+
"callback_context": {
34+
"type": "object",
35+
"title": "Additional context for user defined callback function",
36+
"description": "This object will be passed as the first argument of user-defined callback function",
37+
"default": {},
38+
"examples": [
39+
{
40+
"read_result_from": "/path/to/saved/file",
41+
"force_success": true
42+
}
43+
]
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)