Python IRC机器人赢得';我不能加入某些服务器上的频道,而且我不';我不知道如何让它响应用户的输入
我有一个PythonIRC机器人,我正在开始练习我的python。但是,对于某些服务器,它不会加入通道,尽管它已成功登录。我还想让它对用户命令做出反应,比如!测试,但我不知道怎么做。在它工作的服务器上,它成功地ping回Python IRC机器人赢得';我不能加入某些服务器上的频道,而且我不';我不知道如何让它响应用户的输入,python,irc,bots,Python,Irc,Bots,我有一个PythonIRC机器人,我正在开始练习我的python。但是,对于某些服务器,它不会加入通道,尽管它已成功登录。我还想让它对用户命令做出反应,比如!测试,但我不知道怎么做。在它工作的服务器上,它成功地ping回 from socket import socket, AF_INET, SOCK_STREAM network = raw_input("Server: ") port = 6667 chan = raw_input("Channel: ") nick = raw_input(
from socket import socket, AF_INET, SOCK_STREAM
network = raw_input("Server: ")
port = 6667
chan = raw_input("Channel: ")
nick = raw_input("Nick: ")
irc=socket(AF_INET,SOCK_STREAM)
irc.connect((network, port))
a=irc.recv (4096)
print a
irc.send('NICK ' + nick + '\r\n')
irc.send('USER john_bot john_bot bla :john_bot\r\n')
irc.send('JOIN :' + chan + '\r\n')
irc.send('PRIVMSG ' + chan + ' :Hello.\r\n')
def ircSend(msg):
print(msg)
irc.send(msg)
while True:
data = irc.recv(4096)
print data
if data.find('PING') != -1:
ircSend('PONG ' + data.split()[1] + '\r\n')
在某些服务器上,您必须先用乒乓球响应PING,然后才能实际执行任何操作
ircSend('NICK ' + nick + '\r\n')
ircSend('USER john_bot john_bot bla :john_bot\r\n')
data = ircRecv() # explained later
if data.find('PING') != -1:
ircSend('PONG ' + data.split()[1] + '\r\n')
ircSend('JOIN :' + chan + '\r\n')
ircSend('PRIVMSG ' + chan + ' :Hello.\r\n')
你真的不需要这个
a=irc.recv (4096)
print a
有时IRC服务器一次发送多行(例如MOTD或名称)。只要总字节数不超过4096(某些行将拆分为两行),就可以很好地处理它
如果一行代码会被一分为二(这种情况很少会发生,就像一个PING恰好出现在那里),那么我们可以一次接收一行代码,并将其余字符留给套接字的缓冲区。然而,这可能会有点效率低下(我还没有测试过,所以可能根本没关系)
IRC RFC第8页:
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
NUL or CR or LF>
<crlf> ::= CR LF
::=[':']
::= | [ '!' ] [ '@' ]
::= { } |
::= ' ' { ' ' }
::= [ ':' | ]
:=(特别是第2.3.1节和第4节)。如果您不想处理IRC协议,请在某些服务器上使用Twisted:)在您实际执行任何操作之前,您必须用乒乓球响应PING
ircSend('NICK ' + nick + '\r\n')
ircSend('USER john_bot john_bot bla :john_bot\r\n')
data = ircRecv() # explained later
if data.find('PING') != -1:
ircSend('PONG ' + data.split()[1] + '\r\n')
ircSend('JOIN :' + chan + '\r\n')
ircSend('PRIVMSG ' + chan + ' :Hello.\r\n')
你真的不需要这个
a=irc.recv (4096)
print a
有时IRC服务器一次发送多行(例如MOTD或名称)。只要总字节数不超过4096(某些行将拆分为两行),就可以很好地处理它
如果一行代码会被一分为二(这种情况很少会发生,就像一个PING恰好出现在那里),那么我们可以一次接收一行代码,并将其余字符留给套接字的缓冲区。然而,这可能会有点效率低下(我还没有测试过,所以可能根本没关系)
IRC RFC第8页:
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
NUL or CR or LF>
<crlf> ::= CR LF
::=[':']
::= | [ '!' ] [ '@' ]
::= { } |
::= ' ' { ' ' }
::= [ ':' | ]
:=(特别是第2.3.1节和第4节)。如果您不想处理IRC协议,请使用Twisted:)这是使用套接字的学习练习吗?现实地说,你希望看到一些主要为你做的事情,比如你想要的功能集,并从中衍生出来。Ooops,发布的旧链接-现在是12.1版(虽然8看起来有点滑稽)-这是一个学习使用套接字的练习吗?事实上,你希望看到一些主要为你做的事情,比如你想要的功能集,并从中衍生出来。哦,发布了旧链接-现在是12.1版(虽然8看起来有点滑稽)-
if command == 'PRIVMSG':
destination = line.split()[2] # channel or bot's nick
# <trailing>, in this case, the message
message = line[line[1:].find(':')+2 : ] # everything after the 2nd colon
# we add one since we don't want include the <trailing> colon in
# the message and an other one because line[1:].find() is one smaller
# number than line.find() would be
# if we receive a private message, we have to respond to the sender,
# not to ourself
if destination == nick:
destination = someOneElsesNick
if message.startswith('hi!'):
ircSend('PRIVMSG ' + destination + ' :Hi, '
+ someOneElsesNick + '!\r\n')