Python Forum
learning to setup a simple web server
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
learning to setup a simple web server
#2
Forget all of this,CGI is dead in Python.
A little history Why is WSGI necessary,so Python community did write an all Python solution PEP 3333.
Today is all Python web-frame build on top this solution(WSGI),and CGI is not used at all.

The modern way to do similar stuff to CGI,is to use Flask.
Both Flask and Django has build web-server for local develoment,so not need to write a web-server yourself.
Here a setup i posted before.
Folder setup:
my_app
|-- app.py
  templates\
    |-- index.html
    |-- about.html
  static\
    css\
      |-- style.css
    js\
    img\
app.py:
from flask import Flask, render_template, jsonify, request
 
app = Flask(__name__)
@app.route('/')
def index():
   return render_template("index.html")
 
@app.route('/about',  methods=['GET'])
def about():
   return render_template("about.html") 

if __name__ == '__main__':
   app.run(debug=True)
index.html:
<!doctype html>
<html>
  <head>
     <meta charset="UTF-8">
    <title>Some title</title>
    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
  </head>
  <body>
    <div id="start">
      <h1>Start page</h1>
      <ul>
        <li class="page_1"><a href="/about">About</a></li>
      </ul>
    </div>
  </body>
</html>
about.html:
<div class="foo">
  <h1>The About Page</h1>
  <a href="https://python-forum.io/">Python forum</a>
</div>
style.css:
body { 
  background: #67B26F;  
  background: -webkit-linear-gradient(to right, #4ca2cd, #67B26F);
  background: linear-gradient(to right, #4ca2cd, #67B26F);
  font-size: 100%;
  padding:0; 
  margin:0;		
}

html {
  height: 100%
}

#wrapper {
width: auto;
}
Now from command line in folder where app.py is.
Run python app.py.
Then it look like this:
E:\all_flask\2019\my_app
λ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 334-187-997
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
See address copy that to browser http://127.0.0.1:5000/.
Then should show the page and with a link to about page.
Reply


Messages In This Thread
RE: learning to setup a simple web server - by snippsat - May-02-2019, 11:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020