Re: cgi scripts in Mac OS X

Discussion in 'Python' started by Jeff McNeil, Oct 20, 2007.

  1. Jeff McNeil

    Jeff McNeil Guest

    Your web server needs to be told to execute Python scripts. You can
    handle it a few different ways, depending on your environment.

    1. Place your .py script inside of a ScriptAlias'd /cgi-bin/ directory
    which will force it to be executed.

    2. Rename your .py script to .cgi and add an 'AddHandler cgi-script
    ..cgi' to your Apache configuration

    3. Add an AddHandler cgi-script .py to your Apache configuration.

    This of course assumes that Apache is configured in a way that would
    allow execution and whatnot. Do yourself a favor and give
    '' a read over as it's
    going to cover a lot more than I just did.

    Jeff McNeil, Oct 20, 2007
    1. Advertisements

  2. Jeff McNeil

    James Stroud Guest

    I want to reply to Jeff instead of the original posting, so you don't go
    through all this trouble.

    The apache distro with OS X is configured to execute python cgi scripts
    out of the box, so (2) and (3) above are unnecessary on OS X >= 10.3.

    So, the OS X specific instructions would be

    1. move the script to /Library/WebServer/CGI-Executables:

    sudo mv /Library/WebServer/CGI-Executables/my_script.cgi

    2. make sure its executable

    sudo chmod a+x /Library/WebServer/CGI-Executables/my_script.cgi

    3. add this sh-bang line to the top of the script:

    #! /usr/bin/env python

    4. use the localhost path as the action to your script (ellipses mean
    the rest of the attributes of the form tag):

    <form ... action='http://localhost/cgi-bin/my_script.cgi ...>

    5. make sure that "Personal Web Sharing" is enabled in the Sharing
    Preference Pane

    6. pat self on back for using a REAL operating system and not some
    trash operating system that $ucks real hard:


    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095
    James Stroud, Oct 20, 2007
    1. Advertisements

  3. Jeff McNeil

    Kaushik Roy

    May 21, 2015
    Likes Received:
    Hello I am using Mac OSX mavericks and I am new to apache2 and cgi, but have been using python for a while now.

    My python script called in the cgi-executable folder is:

    import cgi, cgitb
    form = cgi.FieldStorage()
    first_name = form.getvalue('first_name')
    last_name = form.getvalue('last_name')
    print"<title>Hello - Second CGI Program</title>"
    print"<h2>Hello %s %s</h2>"%(first_name, last_name)

    my form.html file is:

    First Name: <inputtype="text"name="first_name"><br/>
    Last Name: <inputtype="text"name="last_name"/>

    But the input from the form doesn't go through to the python script!
    but if i execute localhost/cgi-bin/
    it displays HELLO F L

    please tell me where to place the form.html file so it can find the cgi script

    I have looked at stack overflow a lot and not found any solution. Please help!

    The is in the cgi-executables folder
    Kaushik Roy, May 21, 2015
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.