Python client and CLI for the APE REST API
Project description
PyApe
PyApe is a Python client library and command-line interface (CLI) for interacting with APE server — the Gorille Server REST API.
- 🧰 The
apeCLI tool for quick APE queries directly from your shell - 📦 The
pyapePython library to easily interact with APE directly from your other Python projects
🚀 Quick Start
The ape CLI tool
ape --help
ape static myfile.bin
The pyape Python library
from pyape import Ape
ape = Ape(url="http://localhost", username="user", password="password")
response = ape.post_file_by_upload("myfile.bin")
print(response)
🧰 The ape command line tool
To install the ape command line tool you have two options:
1. Install from PyPi
If you don't have the source code, you can install the latest published version from PyPi:
# Install with pipx directly from PyPi
pipx install pyape-cyd
# Now you can use the CLI command from anywhere
ape --help
2. Install from source code
If you have the source code, you can install directly from the Git repository. It's also usefull if you need to modify the package, this way you can directly see your local modifications.
# Clone the repo
git clone https://gitlab-cyd.lhs.loria.fr/gorille/pyape
cd pyape
# Install PyApe in editable mode from this local directory.
# This lets you make changes and test them immediately without reinstalling.
pipx install -e .
# Now you can use the CLI command from anywhere
ape --help
📦 The pyape Python library
Why using this Python library
PyApe allows you to easily interact with APE from your other Python projects.
Library key features:
- Data validation thanks to Pydantic module:
- Each returned JSON of APE is automatically parsed by Pydantic to "match" a specific Pydantic model. It means that if the format of a APE response change, you need to adjust the model to match with the response structure. It's a data validation feature, it's like a blueprint.
- APE responses as Python objects, Python dictionaries or JSON strings:
- Thanks to Pydantic, you can use APE responses in the format that fit your needs: a real Python object, a dict or a JSON (see example bellow).
- APE error handling with Python exception:
- The correct way to trigger, detect and handle errors in Python is to use Python Exceptions. With this library, if something goes wrong an
ApeExceptionPython exception will be raised. Thanks to this behaviour, you can call APE methods inside a classic try/catch to detect an error (see example bellow).
- The correct way to trigger, detect and handle errors in Python is to use Python Exceptions. With this library, if something goes wrong an
How to import the library in your project
If you want to use PyApe in your Python project the recommended approach is to add it as a Git submodule. This keeps your codebase clean and allows you to edit PyApe independently.
1. Add PyApe as a Git submodule
git submodule add https://gitlab-cyd.lhs.loria.fr/gorille/pyape external/pyape
This clones PyApe inside your project under external/pyape.
2. Add the submodule as an editable dependency in your requirements.txt
In your main project’s requirements.txt, add the line:
-e external/pyape
3. Initialize submodules and install dependencies
After cloning your main project repository, run:
# cd to the root of your Python project
git submodule update --init --recursive
pip install -r requirements.txt
This installs PyApe in editable mode from the local submodule directory.
4. Import and use PyApe in your code
You can now import Ape:
from pyape import Ape
ape = Ape(url="http://localhost", username="user", password="password")
How to use the library inside your project
Simple usage
# Import Ape main class from ape lib
from pyape import Ape
# Create your ape object one time with the URL and the credentials of your instance server
ape = Ape("http://localhost/api", apikey="YOUR_API_KEY")
# Perform a request (e.g. static file analysis)
r = ape.post_file_by_upload("foo.exe")
# The returned variable result is not a dict, it's a Pydantic model (an ApeResponse).
# Because it's a Python object, you can access any attribute of this object (e.g. data attribute).
# Export the response as a dict:
print(magic_result.model_dump())
# Export the response as a valid JSON string:
print(magic_result.model_dump_json())
# For more information about Pydantic models see here:
# https://docs.pydantic.dev/latest/concepts/models/
Using an Ape instance from different locations
In a big project you may need to interact with Ape from multiple locations but you don't want to create a new Ape instance each time you want to request APE.
For this kind of usage you can use a Ape shared session like in the example above.
# File: main.py
from pyape import init_ape_session
# Let's say you project entry point is in the main.py file.
# You can create your Ape instance from here
def main():
init_ape_session("http://localhost/api", apikey="YOUR_API_KEY")
# File: module1.py
from pyape import get_ape_session
# Then, in any other module, you can access your Ape instance created previously.
# Because the instance already exists, you don't need to provide the URL and your credentials.
def my_fun():
ape = get_ape_session()
r = ape.post_file_by_upload("foo.exe")
Handling Ape errors
If something is wrong during the HTTP request the Ape will raise an ApeException exception.
See the example below in order to detect and handle Ape library errors:
from pyape import Ape
ape = Ape("http://localhost/api", apikey="YOUR_API_KEY")
try:
r = ape.post_file_by_upload("foo.exe")
except ApeException as e:
print(e)
🧱 Project Structure
pyape/
├── pyape/
│ ├── __init__.py
│ ├── client.py # Python API wrapper
│ └── cli.py # CLI tool using Typer
├── pyproject.toml # Packaging config
├── README.md
└── ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyape_cyd-0.2.0.tar.gz.
File metadata
- Download URL: pyape_cyd-0.2.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b76871b4956ee7c3ee542a19860da222efc69cec09a110f1ff9e5a3d4cd6f0b3
|
|
| MD5 |
1a4dadbe4e2652181b9f1d9cb16bd86f
|
|
| BLAKE2b-256 |
b7df3675c743ddb51e9798987bd0618b33ed6319b0c97b624f959b272e516e64
|
File details
Details for the file pyape_cyd-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pyape_cyd-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f23082e539cfdfc4eb74e430a2ddc47d6854bce7b187289d4777dfc74c3f8a62
|
|
| MD5 |
77e7c6178754ef52084d31684dc0d73a
|
|
| BLAKE2b-256 |
de92eb6108ec7fa8772cb4576ca12e901d896036f01c631ff1e952ab87b79cb3
|