我复制了一个已经制作的irc脚本并对其进行了一些修改。
# Import some necessary libraries.
import socket
channelList = []
# Some basic variables used to configure the bot
server = "irc.freenode.net" # Server
channel = "#kiwiirc-default" # Channel
botnick = "Mybot" # Your bots nick
def ping(): # This is our first function! It will respond to server Pings.
ircsock.send("PONG :pingis\n")
def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(chan): # This function is used to join channels.
ircsock.send("JOIN "+ chan +"\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
joinchan(channel) # Join the channel using the functions we previously defined
send = True
while 1: # Be careful with these! it might send you to an infinite loop
ircmsg = ircsock.recv(2048) # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
#print ircmsg # Here we print what's coming from the server
if send:
ircsock.send('LIST\r\n')
send = False
x = ircmsg.split()
x = [val for i, val in enumerate(x) if '#' in val]
channelList += x
print len(channelList)
if 'PING :' in ircmsg: # if the server pings us then we've got to respond!
ping()
raw_input()
它将LIST发送到服务器,服务器将发回信息。然后我将它添加到channelList并打印出长度。 当我输入/列入我的irc客户端时,它会返回"找到49400个频道"
最终的频道列表长度应为49400,但总是18,000(忘记确切的数字)