此处的Python noob:在启用Python的Web服务器上,如何使用Python? [英] Python noob here: On a Python enabled web server, how do I use Python?

查看:72
本文介绍了此处的Python noob:在启用Python的Web服务器上,如何使用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!"

我的目标是用Python代替PHP。我对PHP相当满意,可以在自己的本地计算机上使用Python,但无法在Web服务器上使用它。我的网络托管商说他们支持Python,所以我一定做错了。

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.

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

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.

我看了Google的学习Python课程的第一个小时,而且只谈论在本地运行Python。

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.

推荐答案

这是一个完全可以接受的CGI脚本,如果您使用的是Apache,则需要进入cgi-bin,可执行文件(对此我可能是错的),您应该使用语言的通用扩展名 .py .cgi对其进行命名。

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.

如果您可以控制Web服务器考虑使用CGI的文件,则可以将文件放置在所需的任何位置。您可以在此处查看有关Apache中CGI的更多信息: http://httpd.apache.org /docs/2.2/howto/cgi.html

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

基本要点是Apache w除非满足两个条件,否则请勿将文件视为CGI文件: cgi脚本将需要使用 AddHandler SetHandler 指令,以及 ExecCGI 将需要在 Options 指令。通常,这在共享托管环境中对用户不可用。

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.

编辑:为澄清起见,CGI只是一种用于您以任意语言编写脚本的机制,并让您的网络服务器执行它们,并将该脚本的输出发送给您的客户端。除了简单的脚本外,不建议使用这种方法,原因是服务器在每次请求时都会启动您的解释器的新实例。对于Python,最好的解决方案是 WSGI 兼容框架,例如烧瓶 Django 。前两个是微型框架,它们试图不影响您的发展,而Django是一个全栈框架,提供了很多粘合。

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.

这篇关于此处的Python noob:在启用Python的Web服务器上,如何使用Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆