php - Two ways to make python based webpages?


I wanted to try out python to create webpages instead of using php. However I came across that you need either mod_python or mod_wsgi installed to apache to make it play with python. If you now use pure, i'm not sure if it should be said pure, python code, not using any web frameworks like django. I found out that making a simple page looks differently in mod_python and in mod_wsgi.

How come?, the more I looked into python it just seemed to be a harder language to use to make webpages comparing it to php. Is there some good starting point to learn python webdevelopment?

Sorry if my question is blurry. I simply want some guidance to start out with python webdevelopment




Yes, making a webpage with python without using a web framework is harder than it is in php. This is by design, since it gives you a great deal more control over how your page interacts with the server, allowing you to build sites that scale well, among other benefits. WSGI is the modern way to interact with a server, but as you observed, it includes many of the nuts and bolts that PHP hides from the user.

If you're looking for a php-like experience for python, you might look at or Flask. They are pretty minimalistic as far as frameworks go, and take care of interacting with the server but otherwise stay out of your way.

That said, you really should consider Django or another similar framework - they provide some really great benefits that help you get what would otherwise be painfully complex sites going quickly. They solve a slightly different problem and provide different solutions from the common PHP frameworks, so you should consider them even if you don't like frameworks in PHP.

If you want to do things in an even more php-like fashion, you could use CGI. It's definitely not a recommended solution, and won't teach you best practices moving forward, but it can get you started...

Really though, consider a framework. It's how most development in Python for the web is done, and you'll learn more useful skills if you develop using one.




mod_wsgi is better, because it's based on the WSGI specification, which defines the interface between web applications (or frameworks) and web servers. A WSGI app at its simplest is nothing more than a function that sends some HTTP headers via a callback and returns a string in response to information about an HTTP request. And since WSGI is implemented by many web servers, you aren't tied to Apache.

The closest you can get to pure frameworkless web development in Python is to write the WSGI app directly. This will really help you see the things that a framework like Django will obscure.

To make things easier, you might consider using Werkzeug, which is a utility library for WSGI. It has many components that are framework-like, but you can choose which ones you want and which ones you don't. For example, it has a really neat system for parsing and dispatching URLs. Werkzeug also has a simple command-line WSGI server which is probably better for development than Apache.




I'm replying to you with some advice, as someone who was in a very similar situation as you just a few months ago.

So you're using apache to host your website. That's cool. To make python play nice with apache, you're going to want to use mod_wsgi, for the reasons others have stated: seperation of concerns makes it better than cgi and mod_python is no longer being supported.

However, your impression that foregoing a framework will bring you closer to programming in "pure" python is a little bit off the mark. I shared the same opinion, and experimented with both Django and using only mod_wsgi. Let me share what I found.

Mod_wsgi is an implementation of the WSGI standard found in PEP 333. The distinction between the implementation and the standard is important. First, because it means that WSGI compliant applications will work across implementations. More importantly, it reveals something important about what WSGI is meant to do. That is, WSGI is intended a standard for writing frameworks. From the PEP:

simplicity of implementation for a framework author is not the same thing as ease of use for a web application author


The goal of WSGI is to facilitate easy interconnection of existing servers and applications or frameworks, not to create a new web framework.

I'm not saying that you shouldn't do something with wsgi, but you should expect to be writing a framework more than an application. If you're interested in writing a simple framework this tutorial is where I started.

However, if you're just looking to make a website, look into one of the frameworks that others have suggested. You'll still be writing python code, but the authors have worked hard to make the code you write closer connected producing websites than producing frameworks. I've personally used Django, and once it was up and running, it was rather painless to churn out interesting applications. Also, their documentation is very good, and they have a good tutorial here. That being said, Django is very fully featured, and if you're looking for something a little more minimalistic, I've heard good things about Flask, but there are lots of other options as well.




You can use ordinary CGI, which is really simple. Create a Python program that looks something like this:

#!/usr/bin/env python

import sys

sys.stdout.write("Content-type: text/html\r\n\r\n")

print("Hello <em>world</em>!")

Make this file executable (chmod +x) and put it in a directory you've configured for CGI files in your web server.

You will also find the standard Python module very helpful.




If your goal is for making your python program web friendly then the answer is Cherrypy. It is a very flexible and simple framework that enables your python objects exposed in web. Check it out and it has a nice web server built-in that you don't need apache/mod_wsgi etc.,

People are also looking for solutions to the problem: php - Mysql trouble using $_SESSION in program


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.