使用django:代理设置 [英] Working with django : Proxy setup
问题描述
我有一个使用apache的本地开发Django安装程序。问题在于,在部署服务器上没有代理,而在我的工作场所中,我在http代理后面工作,因此请求调用失败。
I have a local development django setup with apache. The problem is that on the deployment server there is no proxy while at my workplace I work behind a http proxy, hence the request calls fail.
有什么方法可以使来自请求库的所有调用都通过代理进行。 [我知道如何使用proxies参数将代理添加到各个呼叫中,但是是否有全局解决方案? ]
Is there any way of making all calls from requests library go via proxy. [ I know how to add proxy to individual calls using the proxies parameter but is there a global solution ? ]
推荐答案
在wsgi文件中添加以下行。
Add following lines in your wsgi file.
import os
http_proxy = "10.10.1.10:3128"
https_proxy = "10.10.1.11:1080"
ftp_proxy = "10.10.1.10:3128"
proxyDict = {
"http" : http_proxy,
"https" : https_proxy,
"ftp" : ftp_proxy
}
os.environ["PROXIES"] = proxyDict
现在您可以在任意位置使用此环境变量,
And Now you can use this environment variable anywhere you want,
r = requests.get(url, headers=headers, proxies=os.environ.get("PROXIES"))
PS -您应该查看以下链接
P.S. - You should have a look at following links
- Official Python Documentation for Environment Variables
- Where and how do I set an environmental variable using mod-wsgi and django?
- Python ENVIRONMENT variables
UPDATE 1
您可以执行以下操作,以便仅在 localhost
。
import socket
if socket.gethostname() == "localhost":
# do something only on local server, e.g. setting os.environ["PROXIES"]
os.environ["PROXIES"] = proxyDict
else:
# Set os.environ["PROXIES"] to an empty dictionary on other hosts
os.environ["PROXIES"] = {}
这篇关于使用django:代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!