本文实例讲述了python简单实现基于ssl的 irc bot。分享给大家供大家参考。具体如下:
#!/usr/bin/python
# -*- coding: utf8 -*-
import socket, string, time, ssl
import urllib, re
network = ‘irc.server.net’
nick = ‘nickname’
chan = ‘bot’
port = 6697
socket = socket.socket(socket.af_inet,socket.sock_stream)
def main(network, nick, chan, port):
socket.connect((network,port))
irc = ssl.wrap_socket(socket)
irc.send(‘nick %s\r\n’ % nick)
print irc.recv(4096)
irc.send(‘user %s %s %s :my bot\r\n’ % (nick,nick,nick))
print irc.recv(4096)
irc.send(‘join #%s\r\n’ % chan)
print irc.recv(4096)
while true:
data = irc.recv(4096)
print data
if data.find(‘ping’) != -1:
irc.send(‘pong ‘+data.split()[1]+’\r\n’)
if data.find(‘!gtfo\r\n’) != -1:
irc.send(‘quit\r\n’)
exit()
print data
if __name__==’__main__’:
main(network, nick, chan, port)
希望本文所述对大家的python程序设计有所帮助。