[英]How to detect which of the two virtual hosts is being used in python and flask
I have a website developed in flask running on an apache2 server that responds on port 80 to two URLs 我有一个在烧瓶中开发的网站运行在apache2服务器上,该服务器在端口80上响应两个URL
Url-1 http://www.example.com 网址1 http://www.example.com
Url-2 http://oer.example.com 网址2 http://oer.example.com
I want to detect which of the two urls the user is coming in from and adjust what the server does and store the variable in a config variable 我想检测用户来自两个URL中的哪个URL,并调整服务器的功能并将变量存储在配置变量中
app.config['SITE'] = 'OER'
or 要么
app.config['SITE'] = 'WWW'
Looking around on the internet I can find lots of examples using urllib2 the issue is that you need to pass it the url you want to slice and I cant find a way to pull that out as it may change between the two with each request. 在Internet上环顾四周,我可以找到许多使用urllib2的示例,问题是您需要将要切片的url传递给它,而我找不到一种将其切出的方法,因为它可能会随每个请求在两者之间发生变化。
I could fork the code and put up two different versions but that's as ugly as a box of frogs. 我可以分叉代码并提出两个不同的版本,但这就像一盒青蛙一样丑陋。
Thoughts welcome. 欢迎思想。
Use the Flask request
object ( from flask import request
) and one of the following in your request handler : 在您的请求处理程序中使用Flask
request
对象( from flask import request
)和以下之一 :
hostname = request.environ.get('HTTP_HOST', '')
url = urlparse(request.url)
hostname = url.netloc
This will get eg oer.example.com
or www.example.com
. 这将获得例如
oer.example.com
或www.example.com
。 If there is a port number that will be included too. 如果有端口号也将包括在内。 Keep in mind that this ultimately comes from the client request so "bad" requests might have it set wrong, although hopefully apache wouldn't route those to your app.
请记住,这最终来自客户端请求,因此“不良”请求可能会将其设置为错误,尽管希望apache不会将那些路由到您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.