Code Monkey home page Code Monkey logo

test-python-server's Introduction

This is a Sample Python Server Application that uses the Flask framework.

Installation

Create and activate a Python virtual environment:

python3 -m venv venv
source venv/bin/activate

Install required Python packages:

pip install -r requirements.txt

Running the application

if you want the application to run on https, you need to have a certificate and key file.

echo "-----BEGIN CERTIFICATE-----
<paste the certificate content here>
-----END CERTIFICATE-----" > certificate.crt
echo "-----BEGIN PRIVATE KEY-----
<paste the certificate content here>
-----END PRIVATE KEY-----" > privatekey.key

Convert to pem format

openssl x509 -in certificate.crt -out certificate.pem -outform PEM
openssl rsa -in privatekey.key -out privatekey.pem -outform PEM

Start the API server using Gunicorn:

gunicorn --bind 0.0.0.0:5000 --certfile=path/to/ssl_cert.pem --keyfile=path/to/ssl_key.pem <python-script>:app

Configure your server's firewall to allow incoming connections on port 5000. For example, using UFW:

sudo ufw allow 5000

Access your API via HTTPS using the following URL format:

https://<your-server-ip>:5000

Running the application as a service

*** Stop the application if it is running

To run the application as a service, you need to create a service file.

sudo nano /etc/systemd/system/api.service
[Unit]
Description=API Service
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/<project-directory>
ExecStart=/home/ubuntu/<project-directory>/venv/bin/gunicorn --bind 0.0.0.0:5000 --certfile=/path/to/ssl_cert.pem --keyfile=/path/to/ssl_key.pem app:app
Restart=always

[Install]
WantedBy=multi-user.target

Start the service

sudo systemctl start api

Enable the service to start on boot

sudo systemctl enable api

Check the status of the service

sudo systemctl status api

Stop the service

sudo systemctl stop api

Disable the service

sudo systemctl disable api

test-python-server's People

Contributors

draftsama avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.