Skip to main content

A minimalist REST API wrapper for python's subprocess API.

Project description

Flask-Shell2HTTP

A minimalist REST API wrapper for python's subprocess API.
Execute shell commands asynchronously and safely from flask's endpoints.

Inspired by the work of awesome folks over at msoap/shell2http.

You can use this for

  • Set a script that runs on a succesful POST request to an endpoint of your choice. See Example code
  • Map a base command to an endpoint and passing dynamic arguments to it. See Example code
  • Can also process uploaded files. See Example code
  • Choose to run a command asynchronously or not. (upcoming feature)

Quick Start

Dependencies

Install

$ pip install flask_shell2http flask_executor

Example

from flask import Flask
from flask_executor import Executor
from flask_shell2http import Shell2HTTP

# Flask application instance
app = Flask(__name__)

executor = Executor(app)
shell2http = Shell2HTTP(app=app, executor=executor, base_url_prefix="/commands/")

shell2http.register_command(endpoint="saythis", command_name="echo")

Run the application server with, $ flask run -p 4000.

Make HTTP calls

$ curl -X POST -d '{"args": ["Hello", "World!"]}' http://localhost:4000/commands/saythis
or using python's requests module,
data = {"args": ["Hello", "World!"]}
resp = requests.post("http://localhost:4000/commands/saythis", json=data)
print("Result:", resp.json())

returns JSON,

{
   "key": "ddbe0a94847c65f9b8198424ffd07c50",
   "status": "running"
}

Then using this key you can query for the result,

$ curl http://localhost:4000/commands/saythis?key=ddbe0a94847c65f9b8198424ffd07c50

Returns result in JSON,

{
  "end_time": 1593019807.782958, 
  "error": "", 
  "md5": "ddbe0a94847c65f9b8198424ffd07c50", 
  "process_time": 0.00748753547668457, 
  "report": {
    "result": "Hello World!\n"
  }, 
  "start_time": 1593019807.7754705, 
  "status": "success"
}

Why?

This was made to integrate various command-line tools easily with IntelOwl.

Various examples

You can find various examples under examples

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Shell2HTTP-1.0.0.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

Flask_Shell2HTTP-1.0.0-py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page