2010-06-29 40 views
5

我正在试验一下IRC服务器/客户端的扭曲库。我发现了一些如何实现IRC客户端的好例子,但似乎在服务器端找到了很好的东西。任何人都可以提供一些有关如何创建一个基本的IRC服务器扭曲的见解吗?扭曲的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方法,但仅仅是摆脱了错误,它没有解决不连接/不工作的问题。

回答

8

也许这样的事?

[email protected]:/tmp/irc-server$ cat > passwd 
alice:secret 
bob:19820522 
[email protected]:/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所调用的代码的功能。您可能想要参考其实现来查看您问题中的代码所针对的成功范例。

+0

那么你是说我正朝着正确的方向发展,但我需要继续添加功能/方法来支持完整的协议?或者从零开始,而不是使用words.protocols.irc.IRC,并使用words.Service?我有点困惑,为什么有twisted.service,然后twisted.irc呢?哪一个是适合的? – themaestro 2010-06-30 12:09:34

+0

twisted.words.service的意图是你可以在其上建立一个IRC服务器。意图与现实之间可能存在差距;这是Twisted的一部分,没有被广泛使用。这并不意味着twisted.words.service不会为你工作,因为这取决于你的目标是什么。 至于为什么有twisted.words.protocols.irc.IRC和twisted.words.service,这很容易 - 前者被用作后者实现的基础。 – 2010-07-01 01:06:56

0

我碰到这本书,它有以下代码,它将运行一个完整的扭曲词语服务器,并允许您创建频道等。以下是本书的代码。

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页,你会发现它的描述。买这本书,这是一个很好的。

0

如果你想要一个简单的“匿名”扭曲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来与twistd -nol- -y irc_server.py然后执行此。

在其他答案中提到的棘手的一点是,扭曲的协议对象上的各种消息消息对它们的输入/返回有期望,所以你必须去模块文档,有时候需要找到源代码那里需要什么。