简体   繁体   中英

Running Python On Server With Mac

I'm trying to run my python files at this address: http://usersignup.drawyourpets.com/ . As you can see, it's displaying the files in the folder but not actually running them ( EDIT : now it just returns a 500 error). When I run the files locally using Google App Engine, it looks like this:

在此输入图像描述

This is correct. And this is what my files look like in FTP (Filezilla):

在此输入图像描述

So I know they're in the right folder.

This has always worked with HTML files, what do I need to do in order to get python running in the browser? Is there a certain line of code that I need to add to my main.py file? I noticed this article:

Python on Mac Web Server

But it doesn't really specify where to add this line of code: AddHandler cgi-script .cgi .py (if that's even what I need to do).

Also, I'm hosting with HostGator, which does support python so I know that's not the problem.

Thanks for your help!

EDIT : Here's the python file I'm trying to run, main.py:

#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.

# See the License for the specific language governing permissions and
# limitations under the License.
#

import webapp2
import cgi

def Build_Page(textarea_content):

form = """
<table>
    <tr>
    <td>
    <label>{0}</label>
    </td>
    <td>
    <label>Username: <input type="text" name="username"/></label>
    </td></tr>

    <tr>
    <td>
    <label>{1}</label>
    </td>
    <td>
    <label>Password: <input type="text" name="password"/></label>
    </td></tr>

    <tr>
    <td> 
    <label>{2}</label>
    </td>
    <td>
    <label>Verify Password: <input type="text" name="verify_password"/>
    </label>
    </td></tr>

    <tr>
    <td>
    <label>{3}</label>
    </td>
    <td>
    <label>Email (optional): <input type="text" name="email"/></label>
    </td></tr>

    </table>
    """
submit = "<input type = 'submit'/>"
form2 = ("<form method='post'>" + form + submit + "
</form>").format("Please enter a username",
"Please enter a password", "Passwords must match","Please enter a valid
email")

header = "<h1>User Signup</h1>" 

return header + form2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        content = Build_Page("")
        self.response.write(content)

    def post(self):
    # look inside the request to figure out what the user typed
        username = self.request.get("username")
        password = self.request.get("password")
        verify_password = self.request.get("verify_password")
        email = self.request.get("email")
    # if the user typed nothing at all, redirect
        if (not username) or (username.strip() == ""):
            error = "Please enter a username."
            self.response.write(error)
            self.redirect("/?error=" + cgi.escape(error, quote=True))

        if (not password) or (username.strip() == ""):
            error = "Please enter a username."
            self.response.write(error)
            self.redirect("/?error=" + cgi.escape(error, quote=True))

        """if (not username) or (username.strip() == ""):
            error = "Please enter a username."
            self.response.write(error)
            self.redirect("/?error=" + cgi.escape(error, quote=True))

        if (not username) or (username.strip() == ""):
            error = "Please enter a username."
            self.response.write(error)
            self.redirect("/?error=" + cgi.escape(error,
            quote=True))"""

        #self.write.form2    
        #message = self.request.get("message") # hello</textarea>hello
        #rotation = int(self.request.get("rotation")) # 0 
        #encrypted_message = caesar.encrypt(message, rotation) 
        #hello</textarea>hello
        #escaped_message = cgi.escape(encrypted_message) 
        # hello&lt;/textarea&gt;hello
        #content = build_page(escaped_message)
        #self.response.write(content)

        #original_form = form.format("","","","","","")
        #page footer

        #class TestHandler(webapp2.RequestHandler):
        #    """ Handles requests coming in to '/add'
        #        e.g. www.user-signup.com/add
        #    """
        #    def get(self):

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    #('/', TestHandler)
], debug=True)

Apparently, according to hostgator, you have to add these lines of code to the top of your .htaccess file:

Addhandler cgi-script .py .pl .cgi
DirectoryIndex main.py 

But that isn't enough to get this python file to run on a HostGator website, because HostGator doesn't support webapp2, which is used several times throughout this file. Now I'm trying to figure out what I can use in place of webapp2 ( See New Question) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM