Getting python to work, Internal Server Error

后端 未结 12 1560
無奈伤痛
無奈伤痛 2021-02-08 11:07

I\'m trying to get Python scripts, called from a web browser, to work. I keep getting the error:

500 Internal Server Error

When I check my err

相关标签:
12条回答
  • 2021-02-08 11:42

    One common error is the wrong path. I my case it was usr/bin/python. The other common error is not transferring the file in ASCII mode. I am using WinSCP where you can set it easily: Go to Options->Preferences->Transfers->click Edit and change the mode to Text.

    This code should work:

    #!/usr/bin/python
    print "Content-type: text/html\n\n";
    print "<html><head>";
    print "<title>CGI Test</title>";
    print "</head><body>";
    print "<p>Test page using Python</p>";
    print "</body></html>";
    
    0 讨论(0)
  • I tried many approaches to get Python working with Apache properly and finally settled with using Apache + mod_WSGI + web.py . It sounds like a lot, but it is much simpler than using the complicated frameworks like Django out there.

    (You're right, don't bother with mod_python)

    Note, I am using Apache2 , but mod_wsgi works on 1.3 as well, based on the modwsgi page.

    If you are on Redhat, I believe you have yum, so make sure to get the apache wsgi module and other python packages:

    $ yum update
    $ yum install gcc gcc-c++ python-setuptools python-devel
    $ yum install httpd mod_wsgi
    

    And get web.py for your version of python. For example, using easy_install. I have v2.6.

    $ easy_install-2.6 web.py
    

    Create a directory for your python scripts : /opt/local/apache2/wsgi-scripts/

    In your httpd.conf :

    LoadModule wsgi_module modules/mod_wsgi.so 
    
    # note foo.py is the python file to get executed 
    # and /opt/local/apache2/wsgi-scripts/ is the dedicated directory for wsgi scripts
    WSGIScriptAlias /myapp /opt/local/apache2/wsgi-scripts/foo.py/
    
    AddType text/html .py
    
    <Directory /opt/local/apache2/wsgi-scripts/>
    
        Order allow,deny
        Allow from all
    </Directory>
    

    Note that web.py uses a "templates directory". Put that into the wsgi directory , /opt/local/apache2/wsgi-scripts/templates/ .

    Create a file /opt/local/apache2/wsgi-scripts/templates/mytemplate.html :

    $def with (text)
    <html>
    <body>
    Hello $text.
    </body>
    </html>
    

    Add appropriate permissions.

    $ chown -R root:httpd   /opt/local/apache2/wsgi-scripts/
    $ chmod -R 770   /opt/local/apache2/wsgi-scripts/
    

    In your python file, foo.py :

    import web
    
    urls = ( '/', 'broker',)
    render = web.template.render('/opt/local/apache2/wsgi-scripts/templates/')
    
    application = web.application(urls, globals()).wsgifunc()
    
    class broker:
        def GET(self):
            return render.mytemplate("World")
    

    The above will replace the special web.py $text variable in the mytemplate with the word "World" before returning the result .

    http://ivory.idyll.org/articles/wsgi-intro/what-is-wsgi.html

    0 讨论(0)
  • 2021-02-08 11:44

    Two things spring immediately to mind.

    1. Make sure you are outputting the Content-Type: text/html header
    2. Make sure you are adding two newlines ("\n") after the headers before you output "Hello, world" or whatever.
    0 讨论(0)
  • 2021-02-08 11:46

    You may also get a better error message by adding this line at the top of your Python script:

    import cgitb; cgitb.enable()

    Also, the header should be capitalized Content-Type, not Content-type, although I doubt that that is breaking anything.

    0 讨论(0)
  • 2021-02-08 11:47

    I had a similar problem, the problem is that you need to have two lines breaks after printing the content type. The following worked for me :

    #!/usr/local/bin/python2.6
    print('Content-type: text/html\r\n')
    print('\r\n')
    print('Hello, World!')
    
    0 讨论(0)
  • 2021-02-08 11:48

    Sounds to me like you're using a script written in Windows on a Unix machine, without first converting the line-endings from 0d0a to 0a. It should be easy to convert it. One way is with your ftp program; transfer the file in ASCII mode. The way I use with Metapad is to use File->FileFormat before saving.

    0 讨论(0)
提交回复
热议问题