带有扭曲网络的 django - wgi 和 vhost

发布于 2024-09-16 03:30:44 字数 1861 浏览 4 评论 0原文

我有一个项目,其目录设置如下:

myproject
  someapp
  sites
      foo
         settings.py - site specific 
  settings.py - global

我正在使用twisted web.wsgi 来为该项目提供服务。我遇到的问题是设置正确的环境。

import sys
import os
from twisted.application import internet, service
from twisted.web import server, resource, wsgi, static, vhost
from twisted.python import threadpool
from twisted.internet import reactor
from django.core.handlers.wsgi import WSGIHandler
from  django.core.management import setup_environ,ManagementUtility
sys.path.append(os.path.abspath("."))
sys.path.append(os.path.abspath("../"))
DIRNAME= os.path.dirname(__file__)
SITE_OVERLOADS = os.path.join(DIRNAME,'sites')

def import_module(name):
 mod = __import__(name)
 components = name.split('.')
 for comp in components[1:]:
  mod = getattr(mod,comp)
 return mod
def buildServer():
 hosts = [d for d in os.listdir(SITE_OVERLOADS) if not os.path.isfile(d) and d != ".svn"]
 root = vhost.NameVirtualHost()
 pool = threadpool.ThreadPool()
 pool.start()
 reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)

 for host in hosts:
  settings = os.path.join(SITE_OVERLOADS,"%s/settings.py" % host)
  if os.path.exists(settings):
   sm = "myproject.sites.%s.settings" % host
   settings_module = import_module(sm)
   domain = settings_module.DOMAIN
   setup_environ(settings_module)
   utility = ManagementUtility()
   command = utility.fetch_command('runserver')
   command.validate()
   wsgi_resource = wsgi.WSGIResource(reactor,pool,WSGIHandler())
   root.addHost(domain,wsgi_resource)
 return root

root = buildServer()
site = server.Site(root)
application = service.Application('MyProject')
sc = service.IServiceCollection(application)
i = internet.TCPServer(8001, site)
i.setServiceParent(sc)

我正在尝试为每个在子目录“sites”中有设置模块的站点设置虚拟主机。然而,似乎每个站点的设置都是共享的。

I have a project which has a directory setup like:

myproject
  someapp
  sites
      foo
         settings.py - site specific 
  settings.py - global

I am using twisted web.wsgi to serve this project. The problem am I running into is setting up the correct environment.

import sys
import os
from twisted.application import internet, service
from twisted.web import server, resource, wsgi, static, vhost
from twisted.python import threadpool
from twisted.internet import reactor
from django.core.handlers.wsgi import WSGIHandler
from  django.core.management import setup_environ,ManagementUtility
sys.path.append(os.path.abspath("."))
sys.path.append(os.path.abspath("../"))
DIRNAME= os.path.dirname(__file__)
SITE_OVERLOADS = os.path.join(DIRNAME,'sites')

def import_module(name):
 mod = __import__(name)
 components = name.split('.')
 for comp in components[1:]:
  mod = getattr(mod,comp)
 return mod
def buildServer():
 hosts = [d for d in os.listdir(SITE_OVERLOADS) if not os.path.isfile(d) and d != ".svn"]
 root = vhost.NameVirtualHost()
 pool = threadpool.ThreadPool()
 pool.start()
 reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)

 for host in hosts:
  settings = os.path.join(SITE_OVERLOADS,"%s/settings.py" % host)
  if os.path.exists(settings):
   sm = "myproject.sites.%s.settings" % host
   settings_module = import_module(sm)
   domain = settings_module.DOMAIN
   setup_environ(settings_module)
   utility = ManagementUtility()
   command = utility.fetch_command('runserver')
   command.validate()
   wsgi_resource = wsgi.WSGIResource(reactor,pool,WSGIHandler())
   root.addHost(domain,wsgi_resource)
 return root

root = buildServer()
site = server.Site(root)
application = service.Application('MyProject')
sc = service.IServiceCollection(application)
i = internet.TCPServer(8001, site)
i.setServiceParent(sc)

I am trying to setup vhosts for each site which has a settings module in the subdirectory "sites". However, it appears that the settings are being shared for each site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装迷糊 2024-09-23 03:30:44

同一 Python 进程中的 Django 项目将共享相同的设置。您需要将它们作为单独的进程生成,以便它们使用单​​独的设置模块。

Django projects within the same Python process will share the same settings. You will need to spawn them as separate processes in order for them to use separate settings modules.

土豪我们做朋友吧 2024-09-23 03:30:44

由于您的目标是一堆无共享虚拟主机,因此尝试以最简单的方式设置流程可能不会让您受益。那么,如何更改 .tac 文件以仅为单个虚拟主机启动服务器,启动大量实例(手动,使用 shell 脚本,使用另一个简单的 Python 脚本等),然后放置反向代理( nginx、Apache,甚至另一个 Twisted Web 进程)位于所有这些进程之前?

可以使用 Twisted 来完成这一切,它甚至可能会带来一些优势,但对于刚刚开始的您可能宁愿专注于您的站点,而不是对部署过程进行细微的调整。如果事情没有更加集成而成为问题,那么就应该重新审视该问题并尝试改进您的解决方案。

Since your goal is a bunch of shared-nothing virtual hosts, you probably won't benefit from trying to set up your processes in anything but the simplest way. So, how about changing your .tac file to just launch a server for a single virtual host, starting up a lot of instances (manually, with a shell script, with another simple Python script, etc), and then putting a reverse proxy (nginx, Apache, even another Twisted Web process) in front of all of those processes?

You could do this all with Twisted, and it might even confer some advantages, but for just getting started you would probably rather focus on your site than on minor tweaks to your deployment process. If it becomes a problem that things aren't more integrated, then that would be the time to revisit the issue and try to improve on your solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文