Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start using MAMP and Python for a PHP developer?

Tags:

python

mamp

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)

like image 451
yaserso Avatar asked Feb 11 '23 19:02

yaserso


1 Answers

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.

like image 116
deceze Avatar answered Feb 13 '23 11:02

deceze