May-02-2019, 04:20 PM
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?