Python API

Code Repository

If you prefer to use GitHub, you can download my code from the following link:

Configuring HTTP API for Unix Server

This section describes how to configure and manage a Flask API service on a Unix server using systemd.

			
# Create a systemd service file for your Flask API
sudo nano /etc/systemd/system/my_flask_api.service

[Unit]
Description=My Flask API Service

[Service]
ExecStart=/usr/bin/python3 /home/kali/test_api/Py_80/http_api.py
WorkingDirectory=/home/kali/test_api/Py_80
User=kali
Group=kali
Restart=always
Access=Internet

[Install]
WantedBy=multi-user.target

# Enable and start the service
sudo systemctl enable my_flask_api
sudo systemctl start my_flask_api

# Check the status of the service
sudo systemctl status my_flask_api
			
		

These commands will allow you to manage your Flask API as a system service, enabling it to start automatically at boot and be controlled using systemd commands.

HTTP API Python

This section shows, Python code for HTTP APIs. You can use this code for various types of testing. This API runs on port number 80 and only requires a password and login. You can find examples in the Postman collection or in the CURL section below.

Curl:

curl -X POST "http://192.168.56.101:80/users" \
  -H "Content-Type: application/json" \
  -u "admin:password" \
  -d '{"name": "lukasz", "email": "lukasz@example.com"}'
  
curl -X GET "http://192.168.56.101:80/users" \
-u "admin:password"

For better convenience, you can use Postman, and here is the collection to import.

Postman collection
Loading code...

Configuring HTTPS API and Certificates for Unix Server

This section describes how to configure a Flask API with HTTPS and SSL certificates on a Unix server using systemd.

				
# Create a systemd service file for your Flask HTTPS API
sudo nano /etc/systemd/system/my_https_flask_api.service

[Unit]
Description=Flask HTTPS API
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/kali/test_api/Py_443/https_api.py
WorkingDirectory=/home/kali/test_api/Py_443
Environment="PYTHONPATH=/home/kali/test_api/Py_443"
SSLContext=/home/kali/test_api/Py_443/cert.pem:/home/kali/test_api/Py_443/key.pem
StandardOutput=journal
StandardError=journal
Restart=always
User=kali
Group=kali

[Install]
WantedBy=multi-user.target

# Set permissions for SSL certificates
cd /home/kali/test_api/Py_443
sudo chmod 600 cert.pem key.pem

# Test the certificate and key files
sudo openssl x509 -in cert.pem -text -noout
sudo openssl rsa -in key.pem -check

# Adjust the service file permissions
sudo chmod 644 /etc/systemd/system/my_https_flask_api.service

# Enable and start the service
sudo systemctl start my_https_flask_api.service
sudo systemctl status my_https_flask_api.service
sudo systemctl enable my_https_flask_api.service
				
			

This configuration sets up your Flask API to use HTTPS with SSL certificates. The systemd service file specifies the paths to the certificate and key files, and ensures the API runs securely on your server.

HTTPS API Python

This section presents Python code for HTTPS APIs. This code can be used for various types of testing. This API runs on port number 443 and requires importing a certeficate into Postman and using a password and login. Examples can be found in the Postman collection or in the CURL section below (this example omits using the certificate).

Curl:

curl -X POST "https://192.168.56.101:443/users" \
  -H "Content-Type: application/json" \
  -u "admin:password" \
  -d '{"name": "lukasz1", "email": "lukasz1@example.com"}' \
  --insecure
  
curl -X GET "https://192.168.56.101:443/users" \
  -u "admin:password" \
  --insecure"

For better convenience, you can use Postman, and here is the collection to import.

Postman collection
Loading code...