Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts

    Using Python for CGI

    I'm having some trouble getting Python working for server-side scripts. Admittedly, I'm a complete CGI and Python n00b.

    So far, I've just been using PHP's passthru() function to execute Python, but I know you can do it directly.

    I've tried this on my GoDaddy server (which is just a $5 economy plan, so that may be why) and on my local machine using LAMPP in htdocs and cgi-bin with no luck.

    My code:
    Code:
    #!/usr/bin/python
    print("Content-type: text/html\n\n")
    print("<h1>Hello.</h1>")
    I've tried "Content-Type" as well. I always get a plain-text representation of the entire source file. I've tried doing chmod a+x to make the python script executable, too. What's the right way to do this?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    At least as far as your local server, is mod_python being loaded by Apache?
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by oracleguy View Post
    At least as far as your local server, is mod_python being loaded by Apache?
    My knowledge of Apache is pretty poor.

    How can I tell if it's being loaded?

    I searched the directory where lampp is stored and couldn't find anything about mod_python.
    Last edited by Dean440; 04-13-2010 at 11:08 PM.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Look in your httpd.conf where it is loading modules (you can tell by all the LoadModule directives), is there one for mod_python.so? If it isn't there double check any other configuration files that httpd.conf is including or just search your system for mod_python.so to see if it even exists.

    What distro are you on?
    OracleGuy

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    I got the problem cleared up by (silly me) changing my scripts to be executable with chmod.

  • #6
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Dean440 View Post
    My code:
    Code:
    #!/usr/bin/python
    print("Content-type: text/html\n\n")
    print("<h1>Hello.</h1>")
    I've tried "Content-Type" as well. I always get a plain-text representation of the entire source file. I've tried doing chmod a+x to make the python script executable, too. What's the right way to do this?
    I don't know about the right way and the wrong way, but what I do (basically) is:

    Code:
    #!/usr/bin/python
    
    # Start printing headers.
    print "Status: 200 OK"
    print "Content-Type: text/plain"
    
    # Print an empty line to end the headers.
    print
    
    # Print the page.
    print """
    <html>
        <head>....
        
        (more of the page here)
        
        ...</body>
    </html>
    """
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •