Finger 的演变:使用单个工厂处理多个协议¶
简介¶
这是 Twisted 教程 从头开始使用 Twisted,或 Finger 的演变 的第八部分。
在本部分中,我们将为 Web 前端添加 HTTPS 支持,展示如何让单个工厂监听多个端口。有关在 Twisted 中使用 SSL 的更多信息,请参阅 SSL 指南 。
支持 HTTPS¶
要编写 HTTPS 站点,我们只需要编写一个上下文工厂(在本例中,它从特定文件加载证书),然后使用 twisted.internet.endpoints.serverFromString
方法构建 SSL 端点。请注意,一个工厂(在本例中为站点)可以使用多个协议监听多个端口。
当然,如果没有 TLS 证书和私钥,这个端点将无法工作。您需要创建一个自签名证书和密钥。这显然不会被您的 Web 浏览器信任,因此您在连接时会看到警告。在这种情况下,请不要担心:您没有风险。
要创建本教程可以使用证书和密钥,请运行以下命令
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
# Do everything properly, and componentize
from zope.interface import Interface, implementer
from OpenSSL import SSL
from twisted.application import internet, service, strports
from twisted.internet import defer, endpoints, protocol, reactor
from twisted.protocols import basic
from twisted.python import components
from twisted.spread import pb
from twisted.web import resource, server, static, xmlrpc
from twisted.words.protocols import irc
class IFingerService(Interface):
def getUser(user):
"""
Return a deferred returning L{bytes}.
"""
def getUsers():
"""
Return a deferred returning a L{list} of L{bytes}.
"""
class IFingerSetterService(Interface):
def setUser(user, status):
"""
Set the user's status to something.
"""
def catchError(err):
return "Internal error in server"
class FingerProtocol(basic.LineReceiver):
def lineReceived(self, user):
d = self.factory.getUser(user)
d.addErrback(catchError)
def writeValue(value):
self.transport.write(value + b"\r\n")
self.transport.loseConnection()
d.addCallback(writeValue)
class IFingerFactory(Interface):
def getUser(user):
"""
Return a deferred returning a string.
"""
def buildProtocol(addr):
"""
Return a protocol returning a string.
"""
@implementer(IFingerFactory)
class FingerFactoryFromService(protocol.ServerFactory):
protocol = FingerProtocol
def __init__(self, service):
self.service = service
def getUser(self, user):
return self.service.getUser(user)
components.registerAdapter(FingerFactoryFromService, IFingerService, IFingerFactory)
class FingerSetterProtocol(basic.LineReceiver):
def connectionMade(self):
self.lines = []
def lineReceived(self, line):
self.lines.append(line)
def connectionLost(self, reason):
if len(self.lines) == 2:
self.factory.setUser(*self.lines)
class IFingerSetterFactory(Interface):
def setUser(user, status):
"""
Return a deferred returning L{bytes}.
"""
def buildProtocol(addr):
"""
Return a protocol returning L{bytes}.
"""
@implementer(IFingerSetterFactory)
class FingerSetterFactoryFromService(protocol.ServerFactory):
protocol = FingerSetterProtocol
def __init__(self, service):
self.service = service
def setUser(self, user, status):
self.service.setUser(user, status)
components.registerAdapter(
FingerSetterFactoryFromService, IFingerSetterService, IFingerSetterFactory
)
class IRCReplyBot(irc.IRCClient):
def connectionMade(self):
self.nickname = self.factory.nickname
irc.IRCClient.connectionMade(self)
def privmsg(self, user, channel, msg):
user = user.split("!")[0]
if self.nickname.lower() == channel.lower():
d = self.factory.getUser(msg.encode("ascii"))
d.addErrback(catchError)
d.addCallback(lambda m: f"Status of {msg}: {m}")
d.addCallback(lambda m: self.msg(user, m))
class IIRCClientFactory(Interface):
"""
@ivar nickname
"""
def getUser(user):
"""
Return a deferred returning a string.
"""
def buildProtocol(addr):
"""
Return a protocol.
"""
@implementer(IIRCClientFactory)
class IRCClientFactoryFromService(protocol.ClientFactory):
protocol = IRCReplyBot
nickname = None
def __init__(self, service):
self.service = service
def getUser(self, user):
return self.service.getUser(user)
components.registerAdapter(
IRCClientFactoryFromService, IFingerService, IIRCClientFactory
)
class UserStatusTree(resource.Resource):
def __init__(self, service):
resource.Resource.__init__(self)
self.service = service
# add a specific child for the path "RPC2"
self.putChild("RPC2", UserStatusXR(self.service))
# need to do this for resources at the root of the site
self.putChild("", self)
def _cb_render_GET(self, users, request):
userOutput = "".join(
[f'<li><a href="{user}">{user}</a></li>' for user in users]
)
request.write(
"""
<html><head><title>Users</title></head><body>
<h1>Users</h1>
<ul>
%s
</ul></body></html>"""
% userOutput
)
request.finish()
def render_GET(self, request):
d = self.service.getUsers()
d.addCallback(self._cb_render_GET, request)
# signal that the rendering is not complete
return server.NOT_DONE_YET
def getChild(self, path, request):
return UserStatus(user=path, service=self.service)
components.registerAdapter(UserStatusTree, IFingerService, resource.IResource)
class UserStatus(resource.Resource):
def __init__(self, user, service):
resource.Resource.__init__(self)
self.user = user
self.service = service
def _cb_render_GET(self, status, request):
request.write(
"""<html><head><title>%s</title></head>
<body><h1>%s</h1>
<p>%s</p>
</body></html>"""
% (self.user, self.user, status)
)
request.finish()
def render_GET(self, request):
d = self.service.getUser(self.user)
d.addCallback(self._cb_render_GET, request)
# signal that the rendering is not complete
return server.NOT_DONE_YET
class UserStatusXR(xmlrpc.XMLRPC):
def __init__(self, service):
xmlrpc.XMLRPC.__init__(self)
self.service = service
def xmlrpc_getUser(self, user):
return self.service.getUser(user)
def xmlrpc_getUsers(self):
return self.service.getUsers()
class IPerspectiveFinger(Interface):
def remote_getUser(username):
"""
Return a user's status.
"""
def remote_getUsers():
"""
Return a user's status.
"""
@implementer(IPerspectiveFinger)
class PerspectiveFingerFromService(pb.Root):
def __init__(self, service):
self.service = service
def remote_getUser(self, username):
return self.service.getUser(username)
def remote_getUsers(self):
return self.service.getUsers()
components.registerAdapter(
PerspectiveFingerFromService, IFingerService, IPerspectiveFinger
)
@implementer(IFingerService)
class FingerService(service.Service):
def __init__(self, filename):
self.filename = filename
self.users = {}
def _read(self):
self.users.clear()
with open(self.filename, "rb") as f:
for line in f:
user, status = line.split(b":", 1)
user = user.strip()
status = status.strip()
self.users[user] = status
self.call = reactor.callLater(30, self._read)
def getUser(self, user):
return defer.succeed(self.users.get(user, b"No such user"))
def getUsers(self):
return defer.succeed(list(self.users.keys()))
def startService(self):
self._read()
service.Service.startService(self)
def stopService(self):
service.Service.stopService(self)
self.call.cancel()
application = service.Application("finger", uid=1, gid=1)
f = FingerService("/etc/users")
serviceCollection = service.IServiceCollection(application)
f.setServiceParent(serviceCollection)
strports.service("tcp:79", IFingerFactory(f)).setServiceParent(serviceCollection)
site = server.Site(resource.IResource(f))
strports.service(
"tcp:8000",
site,
).setServiceParent(serviceCollection)
strports.service(
"ssl:port=443:certKey=cert.pem:privateKey=key.pem", site
).setServiceParent(serviceCollection)
i = IIRCClientFactory(f)
i.nickname = "fingerbot"
internet.ClientService(
endpoints.clientFromString(reactor, "tcp:irc.freenode.org:6667"), i
).setServiceParent(serviceCollection)
strports.service(
"tcp:8889", pb.PBServerFactory(IPerspectiveFinger(f))
).setServiceParent(serviceCollection)