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
#1
I have recently decide to try to learn more about the python programming language. I want to go into the field of computer science and I thought that a good way to stat was to first learn all that I can about python, then I can later go to school and I would at least have knowledge of one programming language to help me. Anyway, so I got this book and I am trying to follow along with it, but I have run into a small problem. The book gives an example of how to set up a simple web page that executes a python script when you click the submit button. But I can't get the server part to work. I have three files: page.html, server.py, and script.py

<html> 
    <title>Web Page</title>
    <body>
        <form method="POST" action="cgi-bin/script.py">
            <P><B>Enter your name:</B>
            <P><input type=text name=user>
            <P><input type=submit>
        </form>
    </body>
</html>
import os, sys
from http.server import HTTPServer, CGIHTTPRequestHandler

webdir = 'C:\\Users\\user\\Desktop\\Python\\Learning_Python\\db\\web'
port = 80

os.chdir(webdir)
srvraddr = ("what do I put here",port)
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()
import cgi
form = cgi.FieldStorage()
print('Content-type: text/html\n')
print('<title>Reply Page</title>')
if not 'user' in form:
    print('<h1>Who are you?</h1>')
else:
    print('<h1>Hello <i>%s</i>!</h>' %cgi.escape(form['user'].value))
When I try to run the server I never get any output from it and if I open the html file in my browser and click the button my script never gets run. You can see at line
srvraddr = ("what do I put here",port)
I have "What do I put here" because I don't know what to put there. The book tells me that it is the hostname but does not give me any idea what I should put there. And if I do try to put anything there it does not do anything or it gives me an error. I don't know what I am doing wrong. Is there something I am missing in my code? Is it my firewall? Can someone help me out?
Reply
#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
#3
As the previous poster said, use flask or django.

However search on google for configuring iOS with python (assuming windows) to use the cgi from the book.
I dont have a link but I think there was a setup tutorial in one of the first links.
As the previous poster said though, things seem to have moved on, and initial usage of flask is pretty quick to set up. There is a tutorial section in these forums too.
Reply
#4
Thank you. I guess the book I have is a little outdated but it is still good. I will just skip most of the web stuff for now and look more into it later. I can try to look for some more info into flask. Thanks for the help.
Reply
#5
Hi Lavacreeperking, Im developing/creating script too and want to put in a simple web page.. Tho my primary target is not to use any framework( not sure if possoble according to above recommendations)...

Anyways, would like to ask what book are you using? And training materials for building webpage and running the script? Thanks
Reply
#6
(May-26-2019, 07:14 AM)searching1 Wrote: Hi Lavacreeperking, Im developing/creating script too and want to put in a simple web page.. Tho my primary target is not to use any framework( not sure if possoble according to above recommendations)...

Anyways, would like to ask what book are you using? And training materials for building webpage and running the script? Thanks
Well it is not a book just for web servers. It is called Programming Python 4th edition(There are most likely newer versions though) I do think the author might have a book on making web servers though. You can try going to the website oreilly.com
Reply
#7
your server address can be left blank... or assign it one-
srvraddr = ("what do I put here",port)
srvraddr = ("",port) # the default will be localhost:80
secondly your web directory pointing to you web page:
webdir = 'C:\\Users\\user\\Desktop\\Python\\Learning_Python\\db\\web'
I believe it should be
webdir = 'C:\Users\user\Desktop\Python\Learning_Python\db\web'  indx.html
if that's where your index.html file is.
It's outdated check out cherrypy or other suggestions.
when you run the server there won't be any output until the web page tries to
access the page. In the url of the browser http://localhost:80 or
http://127.0.0.1:80
Reply


Forum Jump:

User Panel Messages

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