Home/Content/Flask Server

Flask Server

Ranit Saha
July 8, 2024
5 min read

A Python-based web server using Flask, enabling custom domain support and static file serving. Easily configurable via config.py, it allows port-free access by running on port 5000. Ideal for local development mimicking real-world deployment.

Overview

My Server Project is a Python-based web server that allows you to host an HTML website with a customizable domain name. This project is built using Flask, a lightweight WSGI web application framework, to serve static files and make your local development environment mimic a real-world scenario where you can access your site using a custom domain without specifying a port number.

Features

  • Custom Domain Support: Easily configure and access your website using a custom domain name.

  • Static File Serving: Serve HTML, CSS, JavaScript, and other static files from a predefined directory.

  • Port-Free Access: Optionally configure the server to run on port 5000, allowing you to access your site without specifying a port number in the URL.

  • Easy Configuration: Simple and flexible configuration through a single config.py file.

  • Local Development Mimicking Real-World Deployment: Test and develop your website locally as if it were hosted on a real web server with a custom domain.

Getting Started

Prerequisites

  • Python 3.x

  • Flask


Installation

  1. Clone the repository:

git clone https://github.com/coderooz/my_server_project.git
cd my_server_project

  1. Create a virtual environment:

python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

  1. Install Flask:

pip install flask

Configuration

  1. Edit the configuration file:

Open config.py and set your custom domain and static files directory:

class Config:
   SERVER_NAME = "www.myserverproject.proj:5000"
   STATIC_FOLDER = "static_files"

  1. Update your hosts file:

Add the following line to your hosts file to map your custom domain to 127.0.0.1:

  • Windows:

127.0.0.1   www.myserverproject.proj

→ (Located at C:\Windows\System32\drivers\etc\hosts)

  • Mac/Linux:

sudo nano /etc/hosts

→ Add:

127.0.0.1   www.myserverproject.proj

Running the Server

  1. Run the Flask application:

sudo python run.py  # Use sudo only if running on port 5000

  1. Access your website:

Open your browser and go to http://www.myserverproject.proj. You should see your website without specifying a port number.

Changing the Custom Domain or Static Files Directory

To change the custom domain or the static files directory, simply update the config.py file:

class Config:
    SERVER_NAME = "newdomain.proj:5000"
    STATIC_FOLDER = "new_static_folder"

Make sure to update your hosts file accordingly if you change the domain name.

License

This project is licensed under the MIT License - see the https://github.com/coderooz/my_server_project/blob/main/LICENSE file for details.

Contributing

Feel free to fork this repository, create a feature branch, and submit a pull request. All contributions are welcome!

Contact

For any questions or suggestions, please contact [viewersweb02gmial.com].

Comments(0)

Flask Server | Coderooz