Twisted IRC 服务器的好例子?
我正在尝试使用Twisted库来做一些关于IRC服务器和客户端的实验。我找到了一些不错的例子,教我怎么实现一个IRC客户端,但在服务器端的资料却不太好找。有人能给我一些关于如何用Twisted创建一个基本IRC服务器的建议吗?
编辑:那我可以在这个基础上继续吗?我这样做方向对吗?
from twisted.internet.protocol import ServerFactory
from twisted.internet import reactor
from twisted.words.protocols.irc import IRC
class IRCServer(IRC):
def connectionMade(self):
print "client connected"
def handleCommand(self, command, prefix, params):
print "handle comm"
IRC.handleCommand(self, command, prefix, params)
def dataReceived(self, data):
print "data: %s" % data
IRC.dataReceived(self, data)
def irc_unknown(self, prefix, command, params):
print "%s, %s, %s, IRC UNKNOWN" % (prefix, command, params)
def irc_USER(self, prefix, params):
print "USER: %s, %s" % (prefix, params)
def irc_NICK(self, prefix, params):
print "NICK: %s, %s" % (prefix, params)
class IRCServerFactory(ServerFactory):
protocol = IRCServer
factory = IRCServerFactory()
reactor.listenTCP(8002, factory)
reactor.run()
我尝试加入频道时总是失败。我遇到了一个错误,提示没有处理某个命令的函数,所以我写了irc_USER和irc_NICK这两个方法,但这只是消除了错误,并没有解决无法连接或不工作的实际问题。
3 个回答
如果你想要一个简单的“匿名” Twisted IRC 服务器,这基本上是最简单的方法:
from twisted.application import internet, service
from twisted.cred import checkers, portal, credentials
from twisted.cred.checkers import ICredentialsChecker
from twisted.internet import defer
from twisted.words import service as wordsservice
from zope.interface import implements
wordsRealm = wordsservice.InMemoryWordsRealm("example.com")
wordsRealm.createGroupOnRequest = True
wordsRealm.createUserOnRequest = True
class UserAnon:
implements(ICredentialsChecker)
credentialInterfaces = (credentials.IUsernamePassword, credentials.IUsernameHashedPassword)
def __init__(self):
pass
def addUser(self, username, password):
pass
def _cbPasswordMatch(self, matched, username):
return username
def requestAvatarId(self, credentials):
return defer.succeed(credentials.username)
class IRCAnonymous(wordsservice.IRCUser):
def irc_NICK(self, prefix, params):
self.password = 'doesntmatter'
wordsservice.IRCUser.irc_NICK(self, prefix, params)
checker = UserAnon()
portal = portal.Portal(wordsRealm, [checker])
servicefactory = wordsservice.IRCFactory(wordsRealm, portal)
servicefactory.protocol=IRCAnonymous
application = service.Application("ircserver")
ircservice = internet.TCPServer(6667, servicefactory)
ircservice.setServiceParent(application)
然后你可以通过 twistd -nol- -y irc_server.py
来执行这个。
需要注意的是,正如其他回答提到的,Twisted 协议对象上的各种消息对输入和返回值有一些要求,所以你需要查看模块的文档,有时候还得看看源代码,才能搞清楚需要什么。
我看到一本书,里面有一段代码,可以让你搭建一个完整的twisted words服务器,还能创建频道等等。下面就是书里的代码。
from twisted.cred import checkers, portal
from twisted.internet import reactor
from twisted.words import service
wordsRealm = service.InMemoryWordsRealm("example.com")
wordsRealm.createGroupOnRequest = True
checker = checkers.FilePasswordDB("authfile.txt")
portal = portal.Portal(wordsRealm, [checker])
reactor.listenTCP(6667, service.IRCFactory(wordsRealm, portal))
reactor.run()
这本书的链接是:http://books.google.com/books?id=_g5UNxWUKsMC&printsec=frontcover#v=onepage。你可以去第119页查看相关描述。推荐买这本书,内容很不错。
也许可以这样做?
exarkun@boson:/tmp/irc-server$ cat > passwd
alice:secret
bob:19820522
exarkun@boson:/tmp/irc-server$ twistd -n words --irc-port 6667 --auth file:passwd
2010-06-29 11:51:26-0400 [-] Log opened.
2010-06-29 11:51:26-0400 [-] twistd 10.0.0+r29436 (/usr/bin/python 2.6.4) starting up.
2010-06-29 11:51:26-0400 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2010-06-29 11:51:26-0400 [-] twisted.words.service.IRCFactory starting on 6667
2010-06-29 11:51:26-0400 [-] Starting factory <twisted.words.service.IRCFactory instance at 0x9ddbf8c>
如果你想看看这个是怎么实现的,可以查看 twisted/words/tap.py
twisted.words.protocols.irc.IRC
是一个非常基础的IRC服务器解析部分的实现。它并没有实现实际的服务器逻辑,比如频道、模式、消息等等。你可以在这个基础上搭建一个服务器,但几乎得自己从头开始构建整个东西。这正是 twistd words
命令所调用的代码所做的。你可能想参考它的实现,看看它是如何成功实现你问题中代码所指向的目标的。