import socket
irc = 'irc.hack3r.com'
port = 6667
channel = '#chat'
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((irc, port))
sck.send('NICK supaBOT\r\n')
sck.send('USER supaBOT supaBOT supaBOT :supaBOT Script\r\n')
sck.send('JOIN #chat' + '\r\n')
data = ''
while True:
data = sck.recv(4096)
if data.find('PING') != -1:
sck.send('PONG ' + data.split() [1] + '\r\n')
print data
print sck.recv(4096)
When I connect to the server I can't JOIN a channel, I get this Error:
"451 JOIN :You have 开发者_如何学编程not registered"
Mike Graham is wrong. What's wrong is you send the JOIN command too early. It takes a while for the server to register your NICK and USER commands, hence the error "Nick not registered". See this reply: Python IRC bot won't join.
I would also like to encourage you to do continue learning and discovering the IRC protocol by making bots with bare sockets. Who cares your code doesn't comply entirely with RFC 1459. Hardly any server, client or bot complies 100% with the standard. But if it works, it works!
And unlike what Daenyth sais, it isn't too hard to get some great results with a bare socket IRC bot. Just read through the RFC a little and experiment!
It sounds like You are not registered and that is a requirement for joining that channel. You will have to register your nick and then identify before joining.
Also, trying to make an irc bot with bare sockets is not a good idea. This code does not implement RFC 1459 to a useful level and it conflates the logic of your program with your networking. Consider using a networking library (Like Twisted. twisted.words
has a great implementation of the IRC protocol) or writing code that is equivalent to one. (Hint, the former is easier and quicker and less bug prone.)
The particular channel you're trying to join requires you to be registered with the nickserv for that server. Try going on the server with a regular IRC client and creating a channel yourself, and tell the bot to join that.
Python twisted irc client
Installation
sudo yum install python-twisted-words
or
sudo apt-get install python-twisted-words
API Documentation
http://twistedmatrix.com/documents/8.2.0/api/twisted.words.protocols.irc.IRCClient.html
Example
#!/usr/bin/env python2.7
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
class IRCLogger(irc.IRCClient):
logfile = file('/tmp/freenode.txt', 'a+')
nick = 'davey_jones_logger'
def signedOn(self):
self.join('#scala')
def privmsg(self, user, channel, message):
print "Got msg %s " % message
self.logfile.write(" %s said %s \n" % ( user.split('!')[0], message ))
self.logfile.flush()
def main():
f = protocol.ReconnectingClientFactory()
f.protocol = IRCLogger
reactor.connectTCP('irc.freenode.net', 6667, f)
reactor.run()
if __name__ == '__main__':
main()
精彩评论