如何运行Python脚本?
我是一名 PHP 开发人员,我想在项目中使用 python。在 apache 或 lighttpd 这样的服务器上运行 python 脚本的最常见和最简单的方法是什么?我无法理解 PHP 或 ASP 以外的服务器语言如何在服务器上运行。
I am a PHP developer and am I want to use python for a project. What is the most common and simplest way to run python script on a server like apache or lighttpd? I am having trouble understanding how server languages that are not PHP or ASP run on servers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Python 中,我们有一个叫做 WSGI 的东西。但我相信现在这对你来说太过分了。获取一个 Python Web 框架,例如 Django。它配备了一个 Web 服务器,并提供了有关如何稍后部署的良好文档。尝试一下,事情就会开始变得清晰起来。
更长的版本是,Python 是一种通用语言 - 它不像 PHP 那样为网络设计。因此,您需要做一些工作才能让它执行 Web 操作,我们已经为此提供了一些很好的框架(Django 是最容易上手的,所以这就是我向您推荐它的原因)。
一般来说,您应该了解网络的工作原理。它使用 HTTP 作为通信协议,该协议构建在 TCP 堆栈,因此 Web 应用程序只是一个使用套接字的服务器(PHP 拥有它们 作为 以及 Python)并理解 HTTP。 Python 附带了一个内置版本 - SimpleHTTPServer,但它不太适合生产用途(不过,这对开发很有好处)。这就是为什么有诸如 mod_wsgi (特定于 Python)、FastCGI(通用)。这些东西基本上是真正的生产级 Web 服务器(Apache、nginx)与我们的 Python 应用程序对话并向其提供它们获得的 HTTP 的方式。
In Python we have something called WSGI. But I believe this is too much for you right now. Grab a Python web framework, like Django. It comes with a web server and with good documentation on how to deploy it later. Play with it and things will start to clear up.
The longer version is that Python is a general purpose language - it's not designed for the web like PHP is. So you need a bit of work the get it to do web stuff and we already have some good frameworks for that (Django is the easiest to start with, so that is why I'm recommending it to you).
In general you should understand how the web works. It uses HTTP as a protocol for communication, which is build on top of the TCP stack, so a web application is just a server, that uses sockets (PHP has them as well as Python) and understands HTTP. Python comes with one build in - the SimpleHTTPServer, but it is not very good for production uses (it's great for development, though). This is why there are things like mod_wsgi (Python specific), FastCGI (general purpouse). Those things are basically ways for a real, production grade, web server (Apache, nginx) to talk to our python app and feed it the HTTP they get.
您可以安装gunicorn,一个适用于UNIX 的纯Python WSGI HTTP 服务器。 http://gunicorn.org/。
You can install gunicorn a Pure Python WSGI HTTP Server for UNIX. http://gunicorn.org/.
通常,Apache 与 mod_python 一起使用。请参阅此 mod_python 教程
Typically, Apache is used with mod_python. See this mod_python tutorial