【问题标题】:Running Python On Server With Mac使用 Mac 在服务器上运行 Python
【发布时间】:2017-06-20 00:09:26
【问题描述】:

我正在尝试在以下地址运行我的 python 文件:http://usersignup.drawyourpets.com/。如您所见,它显示的是文件夹中的文件,但并未实际运行它们(EDIT:现在它只返回 500 错误)。当我使用 Google App Engine 在本地运行文件时,它看起来像这样:

这是正确的。这就是我的文件在 FTP (Filezilla) 中的样子:

所以我知道它们在正确的文件夹中。

这一直适用于 HTML 文件,我需要做什么才能让 python 在浏览器中运行?是否需要将某行代码添加到我的 main.py 文件中?我注意到这篇文章:

Python on Mac Web Server

但它并没有真正指定在哪里添加这行代码:AddHandler cgi-script .cgi .py(如果这甚至是我需要做的)。

另外,我使用 HostGator 托管,它支持 python,所以我知道这不是问题。

感谢您的帮助!

编辑:这是我要运行的 python 文件,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)

【问题讨论】:

  • 恐怕运行Python不仅仅是通过FTP上传文件。如果 HostGator 如您所说支持 Python,请尝试查找与其托管相关的教程。
  • 我明白了,看起来这可能有我要找的东西:support.hostgator.com/articles/specialized-help/technical/…。它说我需要在 htaccess 文件中添加一行 - 什么是 htaccess 文件/如何创建一个?我在文件夹中没有看到 htaccess 文件。

标签: python macos server


【解决方案1】:

显然,根据 hostgator 的说法,您必须将这些代码行添加到 .htaccess 文件的顶部:

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

但这还不足以让这个 python 文件在 HostGator 网站上运行,因为 HostGator 不支持在整个文件中多次使用的 webapp2。现在我想弄清楚我可以用什么来代替 webapp2 (See New Question).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2022-11-18
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多