4

So I've been developing using PHP and mainly working on WordPress sites. I need to learn python at the moment. I've read online a lot, and although they provide easy tutorials on how to learn the language of python, I need to learn how to get started with its development on my local machine's MAMP server.

Usually for PHP I'd dump an index.php file into the htdocs folder and navigate to it using the URL and it works. I know PHP is the easiest to deploy, as I've learned ASP.NET at university. For Python, though, I've read about the cgi-bin and how MAMP already has mod_wsgi and mod_python installed. I'm not quite familiar with such terms and not familiar with how to start using it on MAMP. I need to know how I can run a website using python. Learning the language is easy, since I can google it myself from hereon and then go for Django.

For example, it would output hello world if I used this piece of code:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

However, this code gives me an error:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!"

Said error showing up in the logs is:

mod_wsgi (pid=1794): Target WSGI script '/Applications/MAMP/htdocs/test/index.py' does not contain WSGI application 'application'.

As you can see I'm oblivious to what happened there. I didn't touch any MAMP configuration files, yet.

TL;DR: I want to know how to open up a simple Python generated page on my local MAMP server. I have no idea how it works. Where do I go to learn? (I don't want to learn the language, yet)

5
  • 1
    While you can run Python as a web server WSGI module, you can also simply run a Python program as its own standalone web server. E.g. $ python my_script.py, then open your browser. Look at Tornado for a simple Python web server/framework to get started for example. Commented Mar 1, 2015 at 19:28
  • 1
    I don't quite understand your question. You're saying that you have a piece of code which works and a piece of code that doesn't work. The second piece of code doesn't work because it uses the wrong API (you can't use a CGI application with a WSGI server). Why don't you simply keep going with the API that does work? Commented Mar 1, 2015 at 19:30
  • @yole See, I have no idea what you just meant. I need to learn that. Usually when I Google Web python all I get is python language tutorials. What are cgi and wsgi servers for a dummy? Or maybe I'm not using the right keywords? Commented Mar 1, 2015 at 19:36
  • 1
    First Google result for WSGI is this page which contains all the relevant information. For CGI, the Wikipedia page is also pretty helpful. Commented Mar 1, 2015 at 19:37
  • 1
    Quite simply, WSGI is a protocol - it specifies an API for web servers and Python processes to interact. CGI is also a protocol, but a different protocol. WSGI assumes that the server code and the served code can call each other (it requires that the served code provide a callable which takes the request environment and the response callback from the server code). CGI assumes nothing except a normal UNIX environment (it sends data over via environment variables and STDIN and expects the served code to respond on STDOUT). Commented Mar 1, 2015 at 19:58

2 Answers 2

4

First of all: what is a web server? It's a program that runs persistently, listens to incoming HTTP traffic on a specific port, and handles incoming HTTP requests by parsing them and delegating them to the appropriate handlers. In the typical MAMP setup, Apache is the web server and your PHP scripts are the delegates that produce content.

Now, according to the above definition, you don't need a dedicated web server. All you need is a program which runs persistently, listens on a port and is able to handle HTTP requests and produce responses. Python has many frameworks that allow it to do that by itself. You just start a Python program, it binds itself to a port and produces responses for incoming HTTP requests. For a pretty simple framework in this category, look at Tornado. You can literally take its Hello World example, put it in a file, start it with $ python helloworld.py and you've got yourself a Python web server.

It is possible to let a dedicated web server like Apache handle connection management and delegate Python to pure content production. For this there needs to be a way for the web server to interact with the Python app. CGI is the most basic form of this and closest to PHP's model of interaction: the web server starts a Python script for every incoming request and takes its output as the response. This is very inefficient though, starting and stopping a script every single time has a lot of overhead. Running Python as a WSGI module is more efficient. This starts the Python script once and keeps it running and gives the web server a method to call a specific function in this script for every request. Less overhead, but a decidedly different execution model than PHP.

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

Comments

4

Create a python script, named hellopython (or whatever), no extension:

#!/usr/bin/python
print 'Content-type: text/html\n\n'
print '<html><head></head><body>Hello from Python!</body></html>'

Place it in

/Applications/MAMP/cgi-bin

Make sure it is executable

chmod +x hellopython

View from your MAMP localhost

http://localhost:8888/cgi-bin/hellopython

Outside of the cgi-bin directory, you can do the same, but give it the extension of .cgi

1 Comment

Great answer with all the main points, could also add that the #!/usr/bin/python line can be changed to any version of python you have installed on any part of your disk.

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.