Skip to main content

跨平台、支持 Web 通信的 Python 终端工具

Project description

DecPython

Run Python in a subprocess from your code and get string results, or open a Jupyter-style web terminal. Optional HTTP API with host/port/secret for remote execution. Cross-platform, stdlib only.

Install

pip install -e .

Quick start

from decpython import DecPython

dp = DecPython(gui=False, python='python')
result = dp.send('a = 1\nb = 2\na + b')   # -> '3'
dp.close()

With a web UI: gui=True. Code and results from send() appear in the browser (Shift+Enter or Run).

Host, port, secret (HTTP API)

When you pass port (or use gui=True), a web server is started. DecPython(...) blocks until the server is bound and ready, so you can call send() or the standalone send() immediately after.

  • host — Bind address (default 'localhost'). Use '0.0.0.0' to accept non-local requests.
  • port — Port number, or None / 0 for auto.
  • secret — If set, remote clients must send the same secret (timestamp + HMAC) to POST /exec. If None, no auth; client should use send(..., secret=None).
# Server with no auth
dp = DecPython(gui=False, host='127.0.0.1', port=0, secret=None)
port = dp.port   # ready after init
dp.close()

Standalone send() (remote)

Send a command to a running DecPython server without holding the instance:

from decpython import DecPython, send

dp = DecPython(gui=False, host='127.0.0.1', port=0, secret='key')
out = send('1 + 2', host='127.0.0.1', port=dp.port, secret='key')
# out == {'code': 200, 'data': '3', 'msg': ''}
dp.close()
  • Success: {'code': 200, 'data': '<result>', 'msg': ''}
  • Error: {'code': -1, 'data': '', 'msg': '<error>'}
    Use secret=None when the server was started with secret=None.

See examples/example_remote.py for more.

API

  • DecPython(gui=..., python='python', host='localhost', port=None, secret=None) — Blocks until the web server (if any) is bound. port / gui control whether the server starts.
  • dp.send(code) — Run code in the subprocess; returns the last expression as a string (or the error message).
  • dp.port — Bound port (None if no server).
  • dp.close() — Stop the subprocess and web server.
  • send(command, host='localhost', port=..., secret=None) — POST to a running server; returns the dict above.

License

MIT

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

decpython-1.0.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

decpython-1.0.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file decpython-1.0.1.tar.gz.

File metadata

  • Download URL: decpython-1.0.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for decpython-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a569b82e7f41c781f1813aa6e3e8988bd1305f0dbfc28fc848a539fb577d77a3
MD5 8a82483f22136284fa5f788f5761cd4f
BLAKE2b-256 211882e777f047948471ed695b46eaa6aefcdd67b45473c9ed2ebb949aade483

See more details on using hashes here.

File details

Details for the file decpython-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: decpython-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for decpython-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4d38ced21f647adce39f3974388e74ece4411f15cf6d813dc0039e040f43dc6d
MD5 a058bcf28f2162b1211572151b7fd038
BLAKE2b-256 ec8957930518b0dc7368faaa091f0bbce8df4e69af9aedafa405de8d6aad8fdc

See more details on using hashes here.

Supported by

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