Web Python 问题

发布于 2024-10-09 11:31:01 字数 299 浏览 4 评论 0原文

任何人都可以帮助我在 Hostgator 共享主机上运行 Python 脚本吗?我主要使用 PHP,但也喜欢 Python,并且想尝试将其应用到网络上。我运行 Python 的唯一方法是使用解释器,或者通过终端使用 >Python file.py。我尝试将 hello world 文件上传到网络服务器,但它输出的只是脚本源。我与 Hostgator 交谈过,但他们只能告诉我我需要使用调度程序,但我似乎找不到这样的例子。我想知道的是如何制作

Hello

输出到浏览器。

谢谢,如果我胡言乱语的话,很抱歉,我整个星期都在断断续续地用谷歌搜索这个问题。

Can anyone assist me in getting a Python script running on Hostgator Shared hosting? I work with PHP mostly, but have taken a liking to Python, and would like to try to get it going on the web. The only way I've ever ran Python is with either the interpreter, or through a terminal, with >Python file.py. I tried just uploading a hello world file to the webserver, but all it outputs is the script source. I talked with hostgator, but all they could tell me was I need to use a dispatcher, which I cannot seem to find an example of. All I want to know, is how to make a <p>Hello</p> output to the browser.

Thanks, and sorry if I'm rambley, I've been Googling this off and on all week now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烧了回忆取暖 2024-10-16 11:31:01

好吧,我得到了 来自 Hostgator 自己的支持网站。

假设您的主机运行的是 Python 2.x,那么您可以按如下方式修改链接到的脚本:

#!/usr/bin/python
print "Content-type: text/html\r\n\r\n"
print "<html><head>"
print "<title>CGI Test</title>"
print "</head><body>"
print "<p>Test page using Python</p>"
print "</body></html>"

并将其放入权限为 755 的 cgi-bin 文件夹中。

更新:Bottle,了解适合单个 Python 模块的简单调度程序。您可以使用 CGI 来部署它,

import bottle
# Put your bottle code here, following the docs on the Bottle site
bottle.run(server=bottle.CGIServer)

进一步更新:除了 Bottle 文档之外,我建议您阅读 有关 CGI 的 Python 文档

Well, I got this from Hostgator's own support site.

Assuming your host is running Python 2.x, then you can adapt the linked-to script as follows:

#!/usr/bin/python
print "Content-type: text/html\r\n\r\n"
print "<html><head>"
print "<title>CGI Test</title>"
print "</head><body>"
print "<p>Test page using Python</p>"
print "</body></html>"

and put it in your cgi-bin folder with permissions of 755.

Update: In terms of "getting around" the cgi-bin folder: that'll depend on options your hosting package allows. Look at Bottle for a simple dispatcher which fits in a single Python module. You can deploy it using CGI,

import bottle
# Put your bottle code here, following the docs on the Bottle site
bottle.run(server=bottle.CGIServer)

Further update: Apart from the Bottle docs, I suggest you read the Python docs about CGI.

简单气质女生网名 2024-10-16 11:31:01

简而言之,您需要修改 .htaccess 文件以允许 Apache 将 python 文件解释为 cgi 脚本,然后使用“#!/usr/bin/python”编写您的 python 脚本,并且不要忘记输出 http 标头(否则您将得到一个500内部错误)

您可以查看这篇文章来获取完整说明

Briefly you need modify your .htaccess file to allow Apache to interpret python file as cgi script, then write your python script with "#!/usr/bin/python" and don't forget to output http header (otherwise you will get a 500 internal error)

You can check this post to get the complete instruction

几味少女 2024-10-16 11:31:01

我不了解 Hostgator,但您希望 Web 服务器将您的 Python 脚本视为可执行的 CGI 脚本。这意味着使其可执行并确保有#!并将其放入 cgi-bin 中,或者将服务器设置为将 .py 识别为 CGI 脚本文件扩展名。 这里是一个简单的示例脚本和一些使用cgi模块的上下文。

I don't know about Hostgator, but you want your web server to see your Python script as an executable CGI script. That means making it executable and ensuring there's a #! in it and either putting it in cgi-bin, or setting your server to recognize .py as a CGI script file extension. Here is a simple example script and some context using the cgi module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文