从 WSGI 脚本获取 URL [英] getting URL from WSGI script

查看:32
本文介绍了从 WSGI 脚本获取 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想根据用户的 URL(或至少是用户的域)从 WSGI 文件调用 Python 代码的不同实例.我们在 Apache 2.x + Python + Django 堆栈上运行.

So I want to invoke different instances of Python code based on user's URL (or at least user's domain) from WSGI file. We're running on Apache 2.x + Python + Django stack.

在 WSGI 脚本(这里此处).然而,这个键丢失了,下面是 os.environ.__dict__ 在一切正常时的样子:

Looks like there should be something like 'SERVER_NAME' or 'HTTP_HOST' key in the os.environ dictionary accessible in WSGI script (here and here). However this key is missing and here's how os.environ.__dict__ looks like when everything is working properly:

{'data': {'LANG': 'C', 'APACHE_LOCK_DIR': 'xxx', 'TZ': 'US/Pacific', 'DJANGO_SETTINGS_MODULE': 'xxx', 'APACHE_RUN_USER': 'xxx', 'PWD': 'xxx', 'APACHE_PID_FILE': 'xxx', 'CELERY_LOADER': 'django', 'APACHE_RUN_DIR': 'xxx', 'APACHE_LOG_DIR': 'xxx', 'APACHE_RUN_GROUP': 'xxx', 'HOME': 'xxx', 'PATH': 'xxx'}}

域/URL 是一个非常基本的信息,我很惊讶我找不到它.

Domain/URL is a very basic info and I'm surprised I'm failing to find it.

更新:我已经决定采用不同的方法(使用 Apache Config 而不是 WSGI 脚本).这似乎解决了缺少 WSGI 环境变量的问题.感谢您的所有回复!

UPDATE: I've settled on a different approach (using Apache Config instead WSGI script). That seems to solve the issue of missing WSGI environment variable. Thanks for all your responses!

推荐答案

你混淆了显示当前系统环境变量的 os.environ dict 和 environ作为参数传递给 wsgi 应用程序.路径在后者.

You're confusing the os.environ dict that shows the current system environment variables with the environ that is passed to the wsgi application as a parameter. The path is in the latter.

但是,由于您使用的是 Django,所有这些信息都可以通过 request 对象获得.

However, since you're using Django, that information is all made available via the request object.

这篇关于从 WSGI 脚本获取 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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