2017-08-28 137 views
0

我在哪裏錯了。據我所知,這應該工作。python irc無法識別irc服務器

import socket, string 

#some user data, change as per your taste 
SERVER = 'irc.freenode.net' 
PORT = 6667 
NICKNAME = 'echoquote' 
CHANNEL = '#python' 
PASSWORD = 'nope' 
import time 
#open a socket to handle the connection 
IRC = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

#open a connection with the server 
def irc_conn(): 
    IRC.connect((SERVER, PORT)) 

#simple function to send data through the socket 
def send_data(command): 
    IRC.send(command + '\n') 

#join the channel 
def join(channel): 
    send_data("JOIN %s" % channel) 

#send login data (customizable) 
def login(nickname, username='user', password = None, realname='Pythonist', hostname='Helena', servername='Server'): 
    send_data("USER %s %s %s %s" % (username, hostname, servername, realname)) 
    send_data("NICK " + nickname) 
    send_data("nickserv identify %s %s\r\n" % (NICKNAME, PASSWORD)) 
    time.sleep(3) 

irc_conn() 
login(NICKNAME) 
join(CHANNEL) 

while (1): 
    buffer = IRC.recv(1024) 
    msg = string.split(buffer) 
    message = ' '.join(msg[3:]) 
    message = ''.join([x for x in message if x in string.printable]) 
    if message: 
     print message + '\n' 
    if msg[0] == "PING": #check if server have sent ping command 
     send_data("PONG %s" % msg[1]) #answer with pong as per RFC 1459 
    if msg [1] == 'PRIVMSG' and msg[2] == NICKNAME: 
     filetxt = open('/tmp/msg.txt', 'a+') #open an arbitrary file to store the messages 
     nick_name = msg[0][:string.find(msg[0],"!")] #if a private message is sent to you catch it 
     message = ' '.join(msg[3:]) 
     filetxt.write(string.lstrip(nick_name, ':') + ' -> ' + string.lstrip(message, ':') + '\n') #write to the file 
     filetxt.flush() #don't wait for next message, write it now! 

回答

0
send_data("nickserv identify %s %s\r\n" % (NICKNAME, PASSWORD)) 

有一個在IRC沒有nickserv命令。這是一些IRC客戶端的別名,它所做的只是向NickServ發送私人消息。 Read the IRC specification,並停止重新發明輪子 - 使用現有的IRC庫,例如。 twisted.words或現有的IRC bot解決方案。