This project demonstrates a client-server application using Django REST Framework (DRF) and ZeroMQ (ZMQ) for communication. The server processes two types of commands:
- OS Commands: Executes system commands like
ping
,ls
, orecho
. - Math Commands: Evaluates basic arithmetic expressions such as
(2 + 3) * 4
.
- Django: Python web framework for building the server-side application.
- Django REST Framework (DRF): Simplifies API development.
- ZeroMQ (ZMQ): A high-performance messaging library for client-server communication.
- Processes OS and Math commands.
- Communicates between client and server using ZeroMQ.
- API endpoint for executing commands.
- Handles invalid commands gracefully with error responses.
- Python 3.8 or higher installed.
- Install required libraries:
pip install django djangorestframework pyzmq
Navigate to the project directory and run the server:
python core/zmq_server.py
The server will start listening on tcp://*:5555
.
Output:
[INFO] Server is running...
[INFO] Received message: {'command_type': 'os', 'parameters': ['echo', 'Hello']}
[INFO] Sending response: {'status': 'success', 'result': 'Hello\n'}
^C
[INFO] Server is shutting down...
[INFO] Server closed.
Start the Django development server:
python manage.py runserver
The API will be available at http://127.0.0.1:8000/api/execute-command/
.
You can send POST requests to the /api/execute-command/
endpoint using Postman, curl, or similar tools.
Request:
POST http://127.0.0.1:8000/api/execute-command/
{
"command_type": "os",
"parameters": ["echo", "Hello, World!"]
}
Response:
{
"status": "success",
"result": "Hello, World!\n"
}
Request:
POST http://127.0.0.1:8000/api/execute-command/
{
"command_type": "compute",
"expression": "(5 + 3) * 2"
}
Response:
{
"status": "success",
"result": 16
}
Run the Django unit tests:
python manage.py test
- Client Sends Command: A JSON request is sent to the
/api/execute-command/
endpoint. - API Processes Request: The Django view sends the command to the ZeroMQ server.
- Server Executes Command: The ZeroMQ server processes the command (OS or Math) and returns the result.
- Response Sent Back: The API sends the response back to the client.
- unit tests: Implemented unit tests for the client and server in
tests.py
. - Logging: Added command logging in
zmq_server.py
.