gpt4 book ai didi

Python 和解析 IRC 消息

转载 作者:太空狗 更新时间:2023-10-29 22:06:10 25 4
gpt4 key购买 nike

根据 RFC,使用 Python 解析从 IRC 服务器接收到的消息的最佳方法是什么?我只是想要某种列表/任何东西,例如:

:test!~test@test.com PRIVMSG #channel :Hi!

变成这样:

{ "sender" : "test!~test@test.com", "target" : "#channel", "message" : "Hi!" }

等等?

(编辑:我想在 general 中解析 IRC 消息,而不仅仅是 PRIVMSG 的)

最佳答案

查看 Twisted 的实现 http://twistedmatrix.com/

很遗憾我没时间,也许其他人可以将它粘贴在这里。

编辑

好吧,我回来了,奇怪的是还没有人粘贴它,所以在这里:

http://twistedmatrix.com/trac/browser/trunk/twisted/words/protocols/irc.py#54

def parsemsg(s):
"""Breaks a message from an IRC server into its prefix, command, and arguments.
"""
prefix = ''
trailing = []
if not s:
raise IRCBadMessage("Empty line.")
if s[0] == ':':
prefix, s = s[1:].split(' ', 1)
if s.find(' :') != -1:
s, trailing = s.split(' :', 1)
args = s.split()
args.append(trailing)
else:
args = s.split()
command = args.pop(0)
return prefix, command, args

parsemsg(":test!~test@test.com PRIVMSG #channel :Hi!")
# ('test!~test@test.com', 'PRIVMSG', ['#channel', 'Hi!'])

此函数严格遵循 IRC RFC 中描述的 EBNF。

关于Python 和解析 IRC 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/930700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com