【发布时间】:2017-06-20 09:32:24
【问题描述】:
我正在尝试在我创建的子域上运行 python 文件。根据 HostGator,支持 python 文件,但不支持我在整个文件中使用的 webapp2 框架:
#!/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</textarea>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)
据我了解,webapp / webapp2 是使 Handlers + 其内部的功能起作用的原因。例如,使用 webapp2 可以在“用户名”标签中获取用户名输入。
由于 HostGator 不支持 webapp2,我还能用什么来代替它? HostGator 的技术支持人员建议我使用 PIP 或 PyPI:
但是在我安装了这些软件包之一并将其文件夹复制到我的子域的目录之后,我怎么知道在文件顶部使用什么命令。我只是说
import pip
和class MainHandler(pip.RequestHandler):,
class TestHandler(pip.RequestHandler):等?
这里是我要托管的地方:
usersignup.thehamburgercollection.com
更新:我无法在 appspot.com(谷歌云)上托管这个应用程序。我只是不知道如何将我的文件上传到正确的目录 - 说明不清楚/youtube 教程已过时。如果/当我弄清楚如何做时,我会发布一个解决方案。
【问题讨论】:
-
pip是一个用于安装 Python 库的命令行工具。这不是你应用程序的一部分。 -
你需要 webapp2 吗?尝试查看 Google App Engine。也许 Heroku
-
@duskwuff 因此,如果是关于下载/安装 WebPy,我只需要在将 WebPy 文件夹放在我的子域文件夹中之后,找出要在文件中使用的“命令”......aka我可以使用什么来代替文件中使用的“webapp2”。
-
@cricket_007 我一直在使用 webapp2 和 Google App Engine,但这只能让我在本地运行我的文件(我认为)。老实说,我只是想将文件托管在服务器上,因为我需要有关文件的帮助 - 我认为如果它托管在网站上,向所有人展示它正在做什么会更容易。
-
是的。我在
appspot.com的公共域上有 10 个 appengine webapp2 应用程序。全部免费