简体   繁体   English

如何检测python和flask中正在使用的两个虚拟主机中的哪个

[英]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.comwww.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.

相关问题 如何知道Python当前使用的是哪个GPU器件? - How to know which GPU device is currently being used in Python? Python Flask-使用哪个文件记录日志? - Python Flask - Which file is used to log? Python和Gtk - 正在使用哪个GTK版本? - Python and Gtk - which GTK version is being used? Python - 检查正在使用哪个子 class - Python - Check which child class is being used 如何在jupyter笔记本中指定使用哪个python和哪个模块? - How do I specify which python and which modules are being used in my jupyter notebook? Python Flask两个站点和使用Paramiko库错误的虚拟环境 - Python Flask two sites and Virtual Environment using Paramiko Library Error 如何ping连接到在两个不同的远程控制器下的mininet中创建的两个不同的虚拟交换机的两个虚拟主机 - how to ping two virtual hosts connected to two different virtual switches created in mininet under two different remote controllers Apache用虚拟主机执行python个脚本 - Apache with virtual hosts to execute python scripts Python3 Http Web服务器:虚拟主机 - Python3 Http Web Server: virtual hosts 虚拟环境可以与两个版本为2.7.10和3.0的python一起使用吗? - Can a virtual environment be used with two versions of python say 2.7.10 and 3.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM