Ich bin noch relativ neu in der Materie Python, aber kein Voll-Anfänger im programmieren.
Normalerweise, wenn ich in einer Subklasse ein Objekt aus der Hauptklasse neu definiere, dann funktioniert dies wunderbar, und beim aufrufen wird das neue objekt aufgerufen...
Im folgenden Script allerdings nicht, ich entdecke den Fehler auch nicht...
Code: Alles auswählen
class IRC:
import sys
import socket
from time import sleep
debug = 1 #Set to 1 and you see all traffic between server&client
show_ping_pong = 1 #Set to 1 and you see the PING/PONG...
class IRC:
"""A very low-level implementation of the IRC-Protocol.
It just supports sending raw commands and receiving everything ;)
It is used as a base class, to subclass the IRCClient...
"""
def __init__(self, conn_data, nick):
"""Connects to the server... :-)
Starts to handle every receiving message forever.
"""
#Define the vars more generally...
self.conn_data = conn_data
self.nick = nick
self.ident = nick
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(conn_data)
msg = self.handler()
msg = self.handler()
msg = self.handler()
print 'Sending NICK/USER'
self.send('/notice B01CC68F nospoof')
self.send('NICK %s' %(nick, ))
self.send('USER %s %s %s :%s' %(nick, conn_data[0], nick, 'hu8-IRCBOT'))
self.send('/notice B01CC68F nospoof')
self.send('JOIN :#kapiland')
def send(self, msg):
"""Send the message msg to the server, and adds if necessary the \r\n"""
if debug == 1:
print 'OUTPUT==>%s' %(msg, )
if msg[-1] != 'n' or msg[-2] != '\\':
msg = msg.__add__('\r\n')
try:
self.sock.send(msg)
except:
print 'Can not send message: %s' %(msg, )
def handler(self):
"""Receives a message and returns it."""
buffer = ''
while 1:
x = self.sock.recv(1)
if x != '\n':
buffer = buffer.__add__(x)
else:
#I've got a complete message... :-)
self.raw_handler(buffer)
return buffer
def raw_handler(self, msg):
"""The raw_handler just manages the PING/PONG...but also if debug==1 it outputs every message.
It dos not do something else and should not be used for something else...
"""
if debug == 1:
print 'INPUT==>%s' %(msg, )
if msg[0:6] == 'PING :':
ping_key = msg[6:]
if show_ping_pong == 1:
print 'PING/PONG %s' %(ping_key, )
self.send('PONG :%s' %(ping_key, ))
def handle_forever(self):
"""Calls the handler() ... forever ;-)"""
while 1:
message = self.handler()
#Give it to someone...
self.handle_command(message)
def handle_command(self,msg):
"""This function is called but doesn't do anything.
It should be replaced by another when you subclass the IRC class
"""
#Do nothing, this must be rewritten in the subclass :-)
print '-.-'
class SimpleIRCClient(IRC):
def __init__(self, conn_data, nick):
IRC.__init__(self, conn_data, nick)
self.send('JOIN #_JD_')
print 'START handle_forever()!!!'
self.handle_forever()
def handle_commmand(self, msg):
"""Handles all the commands in a more sophisticated way than IRC.handle_command.msg.."""
print 'HALLLLOOOOOO!!!!!!'
#Zerleg das ganze Zeugs...
#Zuerst mal bis zum ':'
msg_list = msg.split(':')
command_part = msg_list[1]
text_part = msg_list[2]
#Zerschnipsele den Command part...
commandos = command_part.split(' ')
sender = commandos[0]
command = commandos[1]
rest = commandos[2:]
#Privmsg?
if command == 'PRIVMSG':
sender_nick_ = sender.split('!')
sender_nick = sender_nick_[0]
print '%s has sent you: %s' %(sender_nick, text_part)
irc = SimpleIRCClient(("irc.euirc.net", 6667), 'hu9')
Edit (Leonidas): Code in Python-Tags gesetzt.