0

I am learning python at college. I have got a very stupid question. How do i make a web page with python?

I do this and save as test.py

print "Hello test \n";
print "\n"
print "Just testing \n";

When i go to the url I get the same output

print "Hello test \n";
print "\n"
print "Just testing \n";

I am really confused. What am i doing wrong? My hosting is with hostgator. On their web site it says to do like this:

#!/usr/bin/python
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Python</p>";
print "</body></html>";

this gives me the same error.

2
  • 1
    1. You need a web server, 2. That will server the html content. This is i believe is the simplest way: fragments.turtlemeat.com/pythonwebserver.php Commented Jan 19, 2012 at 8:08
  • 1
    You have to configure your web server (e.g. lighpttd or Apache) so that it executes your Python file when you retrieve your URL. Commented Jan 19, 2012 at 8:08

6 Answers 6

2

Try to run this on your local machine first: Making a simple web server in Python.

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

Comments

1

For study you can use: cherrypy or werkzeug. This is simple web frameworks.

Comments

0

Use some web framework like Django, web2py, pylons, Tornado, etc. They provide basic infrastructure for running webpage on localhost as well as well known servers.

Please look at http://wiki.python.org/moin/WebFrameworks for more detail.

3 Comments

no please, he is trying to just understand the basics of python web programming, don't push him to stuff like django and tornado!
A web framework is far beyond what he needs and there's no indication that it would solve his problem.
@zengr I am more than agree with you. :) . Instead, he could read this (docs.python.org/howto/webservers.html).
0

If you open http://yoursite.com/test.py and see it's content, then probably your hosting has no support for python. Check with services list provided for your hosting plan by your hosting provider.

Comments

0

It looks like you have the headers set correctly in the second one which means your web server might not be setup to parse python. If you're running Apache you probably want mod_python installed.

On the other hand, if you are simply trying to open the python file from your drive in a web browser, this will not work. To be properly rendered in a browser it must go through a web server that can interpret python. If you do not have web hosting you can either set some up somewhere or install a web server on your desktop. The python servers linked to in other answers should work and apache is also freely available.

Comments

0

Ok. it works perfect.

There were some problems with whole thing.

1- The problem i was getting with hostgator was:

Windows and Linux use different line endings for text files. Windows uses the CR and LF characters to terminate a line, but Linux just uses the LF character. This can lead to problems when you write a script on Windows and then try to execute it directly on Linux. 

2- I had to add

AddHandler cgi-script .py

to my .htaccess file

I edited again on notepad++ and added htaccess file then it worked.

The problem i was having on local is sorted aswell.

I had apache on memory stick.

All i had to do is

1- add this to apache:

Options Indexes FollowSymLinks ExecCGI

2- add .py to end of this line

AddHandler cgi-script .cgi

3- Add the python path at the beginning of my oy file

#!E:/EasyPHP-5.3.8.1/Python27/python.exe -u

Comments

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.