1

So I am currently trying to get python to output html on a webpage. Its not working, as there is nothing on the screen, and not sure why. Running the server like so:

from http.server import HTTPServer, CGIHTTPRequestHandler

port = 8080

httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

My simple file, which is just to test and make sure I can accomplish this the way I am trying to. I am receiving this form a form that has one input for an email:

#! /usr/local/bin/python3
#This file is used for testing to make sure output to browser is as expected

import cgi
form_data = cgi.FieldStorage()
email = form_data['email'].value

print("<!doctype html> <br />")
print("<html> <head></head><body>")

print("<p>This is the email info: " + email+</p>)
print("</body>")
print("<html>")

I am new to python, and essentially just trying to print this out. I have searched and have yet to find an answer. So I am hoping the distinguished minds of stack overflow can help me out. I don't want a framework, just to be able to print out few things, but not sure why its not working.

2
  • The code as posted has a syntax error and won't run: the </p> should be in quotes. If that's not just a copy-paste error, it could be that the server is catching that error somewhere along the way and just not showing anything. Also, if you view source in your browser, is there anything there? Commented Apr 29, 2012 at 6:59
  • Its a copy paste error. But thank you for pointing it out Commented Apr 29, 2012 at 7:43

2 Answers 2

1

You're not closing your html tag. Change the last one to /html.

Sign up to request clarification or add additional context in comments.

3 Comments

Restarted the server and still the same error. In my experience, those mistakes rarely cause a blank page. In scripting errors, yes, to in html. But thank you for pointing it out.
Where are you storing the cgi in relation to your web server code? According to an example I'm looking at in Programming Python 4th ed, they should be in server script folder\cgi-bin.
At first I tried it just in the main folder. Then I tried that incase that might have been the issues. In either case, the error persists.
0

I have been looking for my answer for about a day and finally found an answer. It was the simplest thing, but something that surprises me no python developer knows. Apparently one needs to add this line before outputting anything:

print('Content-type: text/html \n')

Now I have never dealt with CGI scripts before python, so I am not sure if that is a requirement of all of them. For this reason, I cannot be sure of the actual reason behind it. It must also be important to note that the new character line must be present as it will not work without it as well. Not too sure about that either. I hope someone who has a similar problem, will stumble upon this as it greatly simplifies making a small webpage on python rather than learning a framework, which allows them to focus on the python code.

I would also appreciate anyone who actually knows why the above fix has to be done.

2 Comments

The examples I was looking at had that but I overlooked it partly for the same reason as you, it seems odd that it would cause such a huge issue, and partly because I thought it was equivalent to your doctype html declaration.
I know right. Its such a small thing. Do you have experience in other CGI type languages @sajattack ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.