跳转到主要内容

为Zope 3应用程序集成的Twisted

项目描述

此软件包将Twisted HTTP和FTP服务器集成到Zope 3应用程序设置中。它还定义了经典Zope 3应用程序的脚本骨架。

详细文档

Zope 3应用程序服务器支持

此软件包负责初始化和启动将提供Zope 3应用程序访问的服务器。此软件包高度依赖于Twisted,尽管某些部分可以重用来在其他环境中启动Zope 3应用程序服务器。

服务器类型

Zope 3需要支持许多服务器类型 - HTTP、FTP、带有postmortem调试的HTTP等。所有这些都在ZCML中注册为IServerType实用工具。这允许开发者轻松地为Zope 3开发和注册新服务器。

ServerType 是一个特定于标准Twisted服务器的IServerType实现。 ServerType 的构造函数接受三个参数:创建Twisted IServerFactory 对象的工厂以及要绑定服务器的默认端口和IP地址。

“工厂”参数应该是一个可调用的对象,它期望一个参数,即ZODB实例。由工厂负责在服务器和应用程序之间实现必要的粘合剂。

>>> class TwistedServerFactoryStub(object):
...     def doStart(self): pass
>>> def factory(db):
...     print 'ZODB: %s' %db
...     return TwistedServerFactoryStub()

对于ServerType的另外两个构造函数参数,defaultPort参数指定服务器的默认TCP端口号。而defaultIP参数指定监听的网络接口。您可以指定网络接口的IP地址,或者使用空字符串来监听所有接口。

我们现在可以实例化服务器类型了。

>>> from zope.app.twisted.server import ServerType
>>> st = ServerType(factory, defaultPort=8080)

让我们确保它确实实现了承诺的接口。

>>> from zope.interface.verify import verifyObject
>>> from zope.app.twisted.interfaces import IServerType
>>> verifyObject(IServerType, st)
True

然后,将服务器类型注册为命名实用工具。这些实用工具在解析<server>部分时使用,以创建监听特定端口号的服务器实例。

当您使用服务器类型的create()方法创建服务器实例时,您需要告诉它一个标识名称和一个ZODB数据库对象。IP地址、端口号和backlog计数可以可选地传递给该方法。

>>> db = 'my database'
>>> server = st.create('Example-HTTP', db, port=0)
ZODB: my database
>>> server #doctest:+ELLIPSIS
<zope.app.twisted.server.ZopeTCPServer instance at ...>

如您所见,服务器类型创建了一个Zope特定的TCP服务器,它只是一个简单的标准twisted.internet.TCPServer,在启动时创建一个日志条目。

>>> server.startService()
>>> print log.getvalue()
-- Example-HTTP Server started.
   Hostname: localhost
   Port: 0

当然,您可以为同一服务器类型创建多个实例,并将它们绑定到不同的端口。

>>> server2 = st.create('Example-HTTP-2', db, port=0)
ZODB: my database
>>> server2.startService()
>>> print log.getvalue()
-- Example-HTTP Server started.
   Hostname: localhost
   Port: 0
-- Example-HTTP-2 Server started.
   Hostname: localhost
   Port: 0

有一种特殊的服务器类型是SSL服务器类型;它需要一些额外信息(私钥路径、证书路径和TLS标志)来启动服务器。如果安装了OpenSSL,该设置才会生效。

# >>> from zope.app.twisted.server import SSLServerType # >>> ssl_st = SSLServerType(factory, defaultPort=8443) # # >>> ssl_server = ssl_st.create(‘Example-HTTPS’, db, # … ‘server.pem’, ‘server.pem’) # ZODB: my database # >>> ssl_server #doctest:+ELLIPSIS # <zope.app.twisted.server.ZopeSSLServer instance at …>

服务器工厂

现在,我们当然不会在Python中将实际服务器的设置硬编码。相反,我们使用ZConfig来设置服务器。不幸的是,这意味着我们需要另一个抽象层来设置服务器。基于ZConfig的配置代码创建所谓的ServerFactorySSLServerFactory对象,然后使用服务器类型来创建服务器。

>>> from zope.interface import implements
>>> from zope.app.twisted.interfaces import IServerType
>>> class MyServerType:
...     implements(IServerType)
...     def create(self, name, db,
...                port='unknown', ip='', backlog=50):
...         if not ip:
...             ip = '*' # listen on all interfaces
...         return ('%s server on %s:%d, registered with %s, backlog %d'
...                 % (name, ip, port, db, backlog))
>>> from zope.app.testing import ztapi
>>> ztapi.provideUtility(IServerType, MyServerType(), name='HTTP')
>>> ztapi.provideUtility(IServerType, MyServerType(), name='FTP')

ServerFactory用于连接到ZConfig并创建在<server>部分中指定的服务器实例。它接收一个包含ZConfig<server>部分中指定的设置的section参数。

>>> class ServerSectionStub:
...     type = 'HTTP'
...     address = ('', 8080)
...     backlog = 30
>>> my_section = ServerSectionStub()
>>> from zope.app.twisted.server import ServerFactory
>>> sf = ServerFactory(my_section)

服务器工厂对象知道如何根据ZODB数据库对象创建服务器。名称是类型、IP和端口的组合,这样Twisted代码就可以区分不同的HTTP服务器。

>>> db = 'my db'
>>> print sf.create(db)
HTTP:localhost:8080 server on *:8080, registered with my db, backlog 30

它可以创建多个,使用不同的端口。

>>> my_section.address = ('', 8081)
>>> sf = ServerFactory(my_section)
>>> print sf.create(db)
HTTP:localhost:8081 server on *:8081, registered with my db, backlog 30

这些设置实际上也可以与FTP一起使用。

>>> my_section.type = 'FTP'
>>> my_section.address = ('127.0.0.1', 8021)
>>> sf = ServerFactory(my_section)
>>> print sf.create(db)
FTP:127.0.0.1:8021 server on 127.0.0.1:8021, registered with my db, backlog 30

CHANGES

3.5.0 (2009-07-24)

  • 更新测试以与最新软件包兼容。

3.4.2 (2009-01-27)

  • 修复测试。删除未使用的代码。

  • 将zope.testbrowser添加到ZEO测试的测试依赖项。

  • 删除对ZODB3的不必要依赖。

  • 删除对zope.app.zapi的依赖,用直接导入替换其使用。

  • 将包主页中的“cheeseshop”更改为“pypi”。

3.4.1 (2008-02-02)

3.4.0 (2007-10-27)

  • 首次发布,独立于Zope主树。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

zope.app.twisted-3.5.0.tar.gz (1.7 MB 查看哈希值)

上传时间

支持