Skip to content

Instantly share code, notes, and snippets.

@jpcofr
Created March 6, 2024 19:40
Show Gist options
  • Save jpcofr/554b88c96616d4c629282be74f042dd4 to your computer and use it in GitHub Desktop.
Save jpcofr/554b88c96616d4c629282be74f042dd4 to your computer and use it in GitHub Desktop.
Simple Flask web server

Project Setup and Server Execution

To set up the project and run the server, follow these steps:

  1. Python Environment Setup:

    • Ensure Python is installed on your system. You can download and install Python from the official website if needed.
  2. Install Required Libraries:

    • Install Flask, the web framework used for creating the HTTP server:
      pip install Flask
  3. Project Directory Structure:

    • The project directory structure should include:
      .
      ├── templates
      └── venv
          ├── bin
          ├── include
          └── lib
              └── python3.12
                  └── site-packages
      
  4. Write Python Code:

    • Create a Python script (e.g., app.py) in the project directory with the provided code.
  5. Create HTML Template:

    • Create an HTML template (e.g., index.html) in the templates directory with the provided HTML code.
  6. Activate Virtual Environment:

    • Navigate to the project directory in the terminal and activate the virtual environment:
      source venv/bin/activate
  7. Run the Server:

    • Start the server by executing the Python script:
      python app.py
  8. Access the Webpage:

    • Open a web browser and go to http://127.0.0.1:5000 to access the server.
  9. Execute Commands:

    • Enter a command in the provided form and click "Execute" to run it on the server.
  10. Test and Enhance:

    • Test the server functionality and enhance the aesthetics of the webpage as needed.

Feel free to reach out for further assistance or updates on your progress.

from flask import Flask, render_template, request
import subprocess
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/execute', methods=['POST'])
def execute_command():
command = request.form['command']
output = subprocess.getoutput(command)
return output
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html>
<head>
<title>Command Execution</title>
</head>
<body>
<h1>Execute Command</h1>
<form action="/execute" method="post">
<label for="command">Enter Command:</label><br>
<input type="text" id="command" name="command"><br>
<input type="submit" value="Execute">
</form>
<div id="output"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment