这里是 Python 菜鸟:在支持 Python 的 Web 服务器上,如何使用 Python?

发布于 2024-10-09 20:16:15 字数 512 浏览 0 评论 0原文

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!"

我的目标是用 Python 替代 PHP。我非常擅长 PHP,并且可以在自己的本地计算机上使用 Python,但无法让它在我的 Web 服务器上运行。我的网络主机说他们支持 Python,所以我一定做错了什么。

现在,Python 与 CGI 联系在一起。 python 文件必须进入我的 cgi-bin 文件夹吗?我从未见过带有 py 扩展名或 cgi 扩展名的 Web 文件,我不知道这些东西是如何工作的,我真的只熟悉 PHP。

我观看了 Google 的“学习 Python”课程的第一个小时,其中只讨论了本地运行 Python。

:( 抱歉我太笨了,请修复我。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!"

My goal is to replace PHP with Python. I'm pretty good with PHP and I can use Python on my own local machine, but I can't get it to work on my web server. My web host says they support Python, so I must be doing something wrong.

Now, Python is associated with CGI. Do python files have to go into my cgi-bin folder? I've never seen a web file with a py extension or a cgi extension, I don't know how these things work, I'm really only familiar with PHP.

I watched the first hour of Google's "Learn Python" class and that only talks about running Python locally.

:( Sorry I'm so nub, please fix me.

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

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

发布评论

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

评论(3

泼猴你往哪里跑 2024-10-16 20:16:15

这是一个完全可以接受的 CGI 脚本。如果您使用 Apache,它需要进入您的 cgi-bin,需要可执行(我可能在这方面是错误的),并且您应该使用该语言的通用扩展名 .py,或使用 .cgi

如果您可以控制网络服务器将哪些文件视为 CGI,则可以将文件放置在您想要的任何位置。很可能,你无法控制这一点。您可以在此处查看有关 Apache 中 CGI 的更多信息:http://httpd.apache.org/docs/2.2 /howto/cgi.html

基本要点是 Apache 不会将文件视为 CGI 文件,除非满足两个条件:需要使用 cgi-script 激活code>AddHandler 或 SetHandler 指令,并且需要在 Options 指令中启用 ExecCGI。通常,共享托管环境中的用户无法使用此功能。

编辑:澄清一下,CGI 只是一种机制,让您可以用任意语言编写脚本,让您的网络服务器执行它们并将该脚本的输出发送到您的客户端。除了简单的脚本之外,不推荐使用这种方法,因为服务器在每次请求时都会启动解释器的新实例。对于 Python,最好的解决方案是 WSGI 兼容框架,例如 FlaskBottleDjango< /a>.前两个是微框架,试图不妨碍你,而 Django 是一个全栈框架,提供了大量的粘合剂。

That's a perfectly acceptable CGI script. If you are using Apache, it will need to go into your cgi-bin, need to be executable (I may be wrong on this), and you should name it with the common extension for the language .py, or with .cgi.

If you can control what files your webserver will consider CGI, you can place the file wherever you want. Chances are, you can't control that. You can view more information on CGI in Apache here: http://httpd.apache.org/docs/2.2/howto/cgi.html

The basic gist is that Apache will not treat a file as a CGI file unless two conditions are met: The cgi-script will need to be activated using the AddHandler or SetHandler directives, and ExecCGI will need to be enabled in the Options directive. Typically, this is not available to the user in a shared hosting environment.

EDIT: To clarify, CGI is just a mechanism for you to write scripts in any arbitrary language, and have your webserver execute them and send the output from that script to your clients. It's not a recommended approach for anything but a simple script, due to the fact that on every single request the server will fire up a new instance of your interpreter. For Python, the best solution would be a WSGI compatible framework, such as Flask, Bottle, or Django. The first two are micro-frameworks that try to stay out of your way, while Django is a full-stack framework that provides a whole lot of glue.

梅倚清风 2024-10-16 20:16:15

执行 Web 应用程序的 Python 方式与 CodeIgniter 或 Cake PHP 等有更多共同点。

Django 开始,尝试一下教程。顽固的 PHP 开发人员对 Django 最常见的抱怨是 Django 不允许您混合代码和 HTML - 这是一件好事。

当然,你可以做普通的老式 CGI,但这是困难的方法。

The Python way to do web applications has more in common with something like CodeIgniter or Cake PHP.

Start with Django, just try the tutorial. The most common complaint about Django from die-hard PHP developers is that Django will not let you mix code and HTML - and it IS a good thing.

Of course you can do plain old CGI, but it is the hard way.

携君以终年 2024-10-16 20:16:15

根据您的网络托管公司的不同,#!/usr/bin/env python 行的读取方式可能略有不同。有些在“python”上包含版本号,有些则在不同的目录中包含 python。您需要咨询您的提供商。

此外,您需要授予全世界对该文件的执行(而不是写入)权限,因为您希望人们执行该脚本。

如果您在 cgi-bin 文件夹中上传带有 .cgi 或 .py 扩展名的代码,然后在浏览器中访问它,现在会发生什么?

另外,如果你用谷歌搜索“python cgi”,你会发现很多教程。对于没有大量点击的简单内容,cgi 可以很好地工作,尽管正如其他人所说,在需要时还有其他机制可以提高资源效率。

您不能在同一页面上混合使用 python 和 html,但有一些机制(请参阅 python 和服务器端包含或 SSI)用于编写带有地标的 html 页面,该地标调用您的 python 程序并将来自 python 程序的响应放入其中结果显示的 html。

Depending on your web hosting company, the #!/usr/bin/env python line may need to read a bit differently. Some include a version number on "python" and some have python in a different directory. You'll need to check with your provider.

Also, you need to give the world execute (NOT write) permission on the file, since you are WANTING people to execute the script.

What happens now if you upload that code with a .cgi or .py extension in your cgi-bin folder and then go to it in your browser?

Also, if you google "python cgi" you'll find a lot of tutorials. For simple stuff without tons of hits, cgi will work fine, although as others have said, there are other mechanisms that are far more resource efficient when that's needed.

You can't mix python and html on the same page, but there are mechanisms (see python and server side include or SSI) for writing an html page with a placemark that calls your python program and puts the response from the python program there in the resulting displayed html.

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