Twisted IRC 服务器的好例子?
我正在尝试使用 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 方法,但这只是消除了错误,它没有解决无法连接/无法工作的问题。
I'm in the process of experimenting a bit with the twisted libraries for IRC servers/clients. I've found a few good examples of how to implement an IRC client but seem to find anything good on the server side of things. Could anybody provide some insight into how to create a basic IRC server in twisted?
Edit: What about building off of this? Am I going the right direction here?
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()
When I try to join the channel I am never able to. I was getting an error relating to not having a handler for a command, so I wrote up the irc_USER and irc_NICK methods but that merely got rid of the error, it didn't solve the problem of not connecting/not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的?
如果您想了解这是如何实现的,请参阅 twisted/words/tap.py
twisted.words.protocols.irc.IRC
是 IRC 服务器解析部分的一个非常基本的实现。它没有实现实际的服务器逻辑,例如通道、模式、消息等。您可以在其上构建服务器,但您必须构建几乎整个事物。这正是twistd Words
调用的代码所做的。您可能需要参考其实现,以查看您问题中的代码的目标的成功示例。Perhaps something like this?
If you'd like to see how this is implemented, see twisted/words/tap.py
twisted.words.protocols.irc.IRC
is a very basic implementation of just the parsing parts of an IRC server. It implements no actual server logic such as channels, modes, messages, etc. You can build a server on it, but you have to build almost the whole thing. This is precisely what the code invoked bytwistd words
does. You may want to refer to its implementation to see a successful example of what the code in your question is aiming towards.我偶然发现这本书,其中包含以下代码,它将在twistd Words服务器上运行完整的代码,并允许您创建频道等。这是书中的代码。
该书:http://books.google.com/books? id=_g5UNxWUKsMC&printsec=frontcover#v=onepage
转到第 119 页,您会找到它的说明。买本书吧,很好的。
I came across this book which has the following code which will run a full on twistd words server and allow you to create channels etc. Here's the code from the book.
The book: http://books.google.com/books?id=_g5UNxWUKsMC&printsec=frontcover#v=onepage
Go to page 119 and you'll find it's description. Buy the book, it's a good one.
如果您想要一个简单的“匿名”twisted IRC 服务器,这基本上是最简单的方法:
然后您可以使用
twistd -nol- -y irc_server.py
从 twind 执行此服务器。其他答案中提到的棘手之处在于,扭曲协议对象上的各种消息消息对其输入/返回有期望,因此您必须查看模块文档,有时还必须查看源代码来弄清楚那里需要什么。
If you want a simple 'anonymous' twisted IRC server, this is basically the easiest way to go about it:
You can then execute this from twistd with
twistd -nol- -y irc_server.py
.The tricky bit as mentioned in other answers is that the various message messages on the twisted protocol object have expectations on their input/returns, so you'll have to go to the module documentation, and sometimes the sourcecode to figure out whats needed there.