views:

114

answers:

5

For some quick background, I'm an XHTML/CSS guy with some basic PHP knowledge. I'm trying to dip my feet into the Python pool, and so far understand how to start simple_server and access a simple Hello World return in the same .py file. This is the extent of what I understand though, heh.

How do I integrate the simple_server and your basic XHTML/CSS files? I want to start the server and automagically call, for instance, index.py (does it need to be .py?). Obviously within the index file I would have my markup and stylesheet and I would operate it like a normal site at that point.

My eventual goal is to get a basic message board going (post, edit, delete, user sessions). I realize I'll need access to a database, and I know my way around MySQL enough to not have to worry about those portions.

Thanks for the help.

EDIT: Allow me to clarify my goal, as I have been told Python does a LOT more than PHP. My goal is to begin building simple web applications into my pre-existing static XHTML pages. Obviously with PHP, you simply make sure its installed on your server and you start writing the code. I'd like to know how different Python is in that sense, and what I have to do to, say, write a basic message board in Python.

+1  A: 

Take a look at CherryPy. It's a nice http framework.

DrBloodmoney
I will take a look at CherryPy, as well as Django. Thanks!
Whellow
Does CherryPy work well on Google App Engine? Also DrBloodmoney, how did we learn to get along after the bomb?
Nosredna
A: 

It depends on what you want to achieve,

a) do you want to just write a web application without worrying too much abt what goes in the background, how request are being handled, or templates being rendered than go for a goo webframework, there are many choices simple http server is NOT one of them. e.g. use django, turbogears, webpy, cheerpy, pylons etc etc see http://wiki.python.org/moin/WebFrameworks for full list

b) if you want to develope a simple web framework from start so that you understand internals and improve you knowledge of python, then I will suggest use simple http server see

  1. how can you create a URL scheme so that URLs are dispatched to correct python function,
  2. see how can you render a html template e.g. containing place holder variables $title etc which you can convert to string using string.Template

b) would be difficult but interesting exercise to do, a) will get you started and you may be writing web apps in couple of days

Anurag Uniyal
+3  A: 

I would recommend Django.

ecounysis
+3  A: 

The other answers give good recommendations for what you probably want to do towards your "eventual goal", but, if you first want to persist with wsgiref.simple_server for an instructive while, you can do that too. WSGI is the crucial "glue" between web servers (not just the simple one in wsgiref of course -- real ones, too, such as Apache or Nginx [both with respective modules called mod_wsgi] as well as, for example, Google App Engine -- that one offers WSGI, too, as its fundamental API) and web applications (and frameworks that make it easier to write such applications).

Everybody's recommending various frameworks to you, but understanding WSGI can't hurt (since it will underlie whatever framework you eventually choose). And for the purpose of such understanding wsgiref.simple_server will serve you for a while longer, if you wish.

Essentially, what you want to do is write a WSGI app -- a function or class that takes two parameters (an "enviroment" dictionary, and a "start response" callable that it must call back with status and headers before returning the response's body). Your "WSGI app" can open your index.py or whatever else it wants to prep the status, headers and body it returns.

There's much more to WSGI (the middleware concept is particularly powerful), though of course you don't have to understand it very deeply -- only as deeply as you care to! See wsgi.org for tutorials &c. Gardner's two-part article, I think, is especially interesting.

Once (and if that's your choice) you understand WSGI, you can better decide whether you want it all hidden in a higher level framework such as Django (so you can focus on application-level issues instead) or use a very light and modular toolbox of WSGI utilities such as Werkzeug -- or anything in-between!-)

Alex Martelli
This is also very good information... I much appreciate your advice!
Whellow
+1  A: 

"Obviously with PHP, you simply make sure its installed on your server and you start writing the code."

Not true with Python. Python is just a language, not an Apache plug-in like PHP.

Generally, you can use something like mod_wsgi to create a Python plug-in for Apache. What you find is that web page processing involves a lot of steps, none of which are part of the Python language.

You must use either extension libraries or a framework to process web requests in Python. [At this point, some PHP folks ask why Python is so popular. And the reason is because you have choices of which library or framework to use.]

PHP parses the request and allows you to embed code in the resulting page.

Python frameworks -- generally -- do not work this way. Most Python frameworks break the operation down into several steps.

  1. Parsing the URL and locating an appropriate piece of code.

  2. Running the code to get a result data objects.

  3. Interpolating the resulting data objects into HTML templates.

"My goal is to begin building simple web applications into my pre-existing static XHTML pages."

Let's look at how you'd do this in Django.

  1. Create a Django project.

  2. Create a Django app.

  3. Transform your XTHML pages into Django templates. Pull out the dynamic content and put in {{ somevariable }} markers. Depending on what the dynamic content is, this can be simple or rather complex.

  4. Define URL to View function mappings in your urls.py file.

  5. Define view functions in your views.py file. These view functions create the dynamic content that goes in the template, and which template to render.

At that point, you should be able to start the server, start a browser, pick a URL and see your template rendered.

"write a basic message board in Python."

Let's look at how you'd do this in Django.

  1. Create a Django project.

  2. Create a Django app.

  3. Define your data model in models.py

  4. Write unit tests in tests.py. Test your model's methods to be sure they all work properly.

  5. Play with the built-in admin pages.

  6. Create Django templates.

  7. Define URL to View function mappings in your urls.py file.

  8. Define view functions in your views.py file. These view functions create the dynamic content that goes in the template, and which template to render.

S.Lott
This is extremely helpful, I can't express how much I appreciate the time it took for you to write that out for a beginner such as myself. I've actually messed around a bit in Django from a previous freelance job, so I have a good introduction to it already. :)Thanks again!
Whellow