简体   繁体   English

如何为 mod_wsgi (Python) 配置路径并处理请求

[英]How to configure path for mod_wsgi (Python) and process the request

while I have quite some python experience, I've never used it for web and I have vast amount of web experience with PHP.虽然我有相当多的 python 经验,但我从未将它用于 web,而且我有大量 web PHP 的经验。

Now I want to create a simple python script (lets call it service.py ) that runs on example.com .现在我想创建一个简单的 python 脚本(我们称之为service.py ),它在example.com上运行。 I installed mod_wsgi as suggested by the docs, my web server is Apache 2.2, the mod_wsgi is loaded successfully.我按照文档的建议安装了 mod_wsgi,我的 web 服务器是 Apache 2.2,mod_wsgi 加载成功。

How do I configure my web server/mod_wsgi so that requests comming to example.com/service are processed by service.py ?如何配置我的 web server/mod_wsgi 以便service.py处理进入example.com/service的请求?

Then how do I access the request params (like $_GET, $_POST, $_FILES) from the python script?那么如何从 python 脚本访问请求参数(如 $_GET、$_POST、$_FILES)?

With mod_wsgi, you configure which URLs are served by setting WSGIScriptAlias .使用 mod_wsgi,您可以通过设置WSGIScriptAlias来配置提供哪些 URL。 Your script, though, needs to be an actual WSGI application, which exposes an application variable which is called by the server.但是,您的脚本需要是一个实际的 WSGI 应用程序,它公开一个由服务器调用的application变量。

I suspect it'd be easier to configure your script as simple CGI.我怀疑将脚本配置为简单的 CGI 会更容易。 You can then use the cgi module from the standard library to access your request params (note, though, that the examples you give are PHP-specific: they're accessed differently in Python, depending on the specific framework).然后您可以使用标准库中的cgi模块来访问您的请求参数(但请注意,您提供的示例是特定于 PHP 的:它们在 Python 中的访问方式不同,具体取决于特定的框架)。

Another alternative would be to use a mini-framework like Flask, which would encapsulate all this and give you a simple interface to use in your service.py script.另一种选择是使用像 Flask 这样的迷你框架,它将封装所有这些并为您提供一个简单的界面以在您的 service.py 脚本中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM