首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python irc无法识别irc服务器

python irc无法识别irc服务器
EN

Stack Overflow用户
提问于 2017-08-28 21:11:51
回答 1查看 168关注 0票数 0

我在哪里做错了?据我所知,这应该是可行的。

代码语言:javascript
复制
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!
EN

回答 1

Stack Overflow用户

发布于 2017-08-28 21:26:21

代码语言:javascript
复制
    send_data("nickserv identify %s %s\r\n" % (NICKNAME, PASSWORD))

IRC中没有nickserv命令。在某些IRC客户端中,这是一个别名,它所做的一切就是向NickServ发送一条私人消息。Read the IRC specification,停止重复发明轮子-使用现有的IRC库,例如。twisted.words或现有的IRC bot解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45919916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档