where do I begin with web programming in python?

7stud bbxx789_05ss at yahoo.com
Fri May 2 04:34:54 EDT 2008


On May 1, 3:25 pm, jmDesktop <needin4mat... at gmail.com> wrote:
> I have been to the main python site, but am still confused.  I have
> been using .net, so it may be obvious how to do this to everyone
> else.  I am aware there are various frameworks (Django, Pylons, etc.),
> but I would like to know how to create web pages without these.  If I
> have mod_python or fastcgi on apache, where do I start?  I don't have
> clue where to begin to create a web page from scratch in python.  I am
> sure I will want to access database, etc., all the "normal" stuff, I
> just want to do it myself as opposed to the frameworks, for learning.
>
> Thank you for any help.

Directions for a simple CGI script:

1) Start apache.

2) Use a text editor to create a webpage with a link:

<html>
<head>
<title>Python CGI Test</title>
</head>

<body>
<div>
<a href="http://localhost/cgi-bin/first.py">click me</a>
</div>
</body>

</html>

Save that file with a .htm extension anywhere on your computer, e.g.
name the file test.htm and save it in C:\My Documents.


3) The value of the link's href attribute is a special url.  The url
starts with "http://localhost", or it may need to start with something
like http://localhost:8080" depending on what port number you
installed Apache on.  If you used the default port when you installed
Apache, then the first part of the url will be "http:/localhost".

The rest of the url is the relative path to your cgi script.  The path
is relative to your Apache2 folder.  For instance, my directory
structure looks like this:

Apache2
----htdocs
----cgi-bin
--------first.py
----etc.

So the relative path to my cgi script is "/cgi-bin/first.py".

4) Create a cgi script:

#!/usr/bin/env python

#For Windows, instead of the above line use
#something like: #!C:\Python25\python.exe
#instead.  The path after "#!" should be the
#path to wherever python.exe is on your computer.

import cgitb; cgitb.enable()

#The above line will cause error messages to
#be sent to your browser, which is helpful for
#debugging.  Otherwise, your browser will just
#show a blank page when there is an error in your
#script

print "Content-type: text/html"
print
print "<h1>Hello World</h1>"


The first print statement is the minimum header you need when
responding to a web page.  After you print all the headers you desire,
then you need to print a blank line.  After the blank line, you print
the html that you want the browser to display.


5) On Unix: you have to set the file permissions for your cgi script.
Everyone must be able to read and execute your cgi script:

$ chmod 755 first.py


6) Start your web browser, and click on File>Open and navigate to
your .htm file.  When your html page opens in your browser, click on
the link.  The link will call your python cgi script, the cgi script
will respond my sending some html to your browser, then your browser
will display the html.





More information about the Python-list mailing list