Take the 2-minute tour ×

If I want to write a webpage in Python, where do I begin?

  • Do I save my file as "index.py"?
  • Can I mix Python and HTML? what does that look like?


I want to learn how the Python process works on the web. I want to know if I can put Python into an existing webpage and having it render the Python code. I'm not asking about frameworks.


It seems that I'm going to try Google App Engine. Thanks to all those who responded.

share|improve this question
See stackoverflow.com/questions/2023111/… –  gimel Sep 28 '10 at 15:56
welcome to life outside PHP. I'm something of a refugee myself. –  aaronasterling Sep 28 '10 at 20:08

4 Answers 4

up vote 14 down vote accepted

At its most basic level, a Python script works like any CGI program. A Web request is sent to the server. The server sees that the URL is mapped to a Python script, so it runs your program, passing along information about the request (HTTP request headers, etc.). Your script receives those parameters and returns HTTP response headers and, in most cases, a valid HTML page, unless your script is doing a redirect or something rather than an HTML response. All this is done by simply sending the headers and HTML to standard output, e.g. using print or the like, in the appropriate format. Then your script ends.

Because HTTP is a stateless protocol (that is, it does not inherently have any concept of a "user" or a "session"), you have to keep track of users' state data by sending them cookies (or hidden form fields, which is what we used to use before cookies were invented). If you have multiple scripts, you have to arrange to save any information that needs to be passed from one script to another, again using cookies or hidden fields form or special URLs. (You can also save some of that information server-side in a database and simply provide a session cookie.)

Generally you do not embed Python code in HTML as you would with PHP or ASP. Instead your Python script would use a template, possibly reading it from a file, and substitute the necessary values into it.

All this quickly becomes a major pain to do yourself as your application gains complexity (I do recommend it as a learning experience, however -- every Web developer should know how HTTP and CGI work). This is where the frameworks that others are mentioning come in, by doing much of that for you, providing abstractions that make HTTP seem like it has user sessions, templating mini-languages, and so forth.

share|improve this answer
+1, good answer, I wish I could +1 you again for the "every Web developer should know how HTTP and CGI work" part. –  Mark Sep 28 '10 at 18:37
This explains the concept, even though I am going with te App Engine in the end. –  Moshe Sep 29 '10 at 13:28

There are many frameworks you can use. Look for "getting started pages" in the following frameworks:

They all look a bit differently, so choose what you like.

share|improve this answer

You can do a pure cgi solution, if you're not looking for frameworks:


Looks pretty good, but I haven't researched extensively.

For any real application, you're better off using a framework like django.

share|improve this answer

well you dont "NEED" Django, but it does makes it easier.. it's a good way to start.. i've never developped webs in python and django was really helpful

where do I begin?

Like anything else in programming: RTFD and STFW

Do I save my file as "index.py"?

In Django nop, you'll need to create some files (views, models and templates) but you can create a "template" for your index.html


Yes we can!.. but it looks uglier than a monkey sucking lemons:

html = "<html><body>"
html += "<p>hello world</p>"
return HttpResponse(html)

(i really dont like mixing html to any language)

Good Luck

EDITED if you rather not use frameworks.. read this, i think this is what you want http://wiki.python.org/moin/CgiScripts

share|improve this answer
What if I'm not using Django? I wanna try a Python on a GoDaddy.com linux server. –  Moshe Sep 28 '10 at 16:13
i dont have the answer to that.. =) i tried to develop without using frameworks in python and it was too complicated for me =P its not as easy as php –  pleasedontbelong Sep 28 '10 at 16:30

Your Answer


