Here is the complete code:
#client der mit irc-server kontakt aufnimmt
import time
import socket
from sys import argv
script, mitspieler1, mitspieler2 = argv
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class funktion:
def main(self):
print "Socket(Client)"
host = "irc.iz-smart.net"
port = 6667
PASS = "7987fshd"
NICK = "Testikus"
USER = "Testikus localhost irc.iz-smart.net :Testikus"
self.login(PASS, NICK, USER, host, port)
print "Verbindung aufgebaut zu {0}(IP:{1})".format(
host, socket.gethostbyname(host)
)
self.haupt_schleife()
def haupt_schleife(self):
while True:
antwort = sock.recv(4096)
join = "JOIN #testblablub \r\n"
print antwort
if antwort[0:4] == "PING":
self.pong(antwort, join)
elif antwort.split()[3] == ":quiz.start":
sock.sendall("PRIVMSG #testblablub Es spielen mit: "
+mitspieler1+" und "+mitspieler2+"\r\n"
)
time.sleep(2)
self.fragen(antwort)
def pong(self, antwort, join):
sock.sendall("PONG " + antwort.split()[1] + "\n")
time.sleep(3)
sock.sendall(join)
sock.sendall("PRIVMSG #testblablub hi \r\n")
def login(self, PASS, NICK, USER, host, port):
sock.connect((host, port))
sock.sendall("PASS "+PASS+"\n")
sock.sendall("NICK "+NICK+"\n")
sock.sendall("USER "+USER+"\n")
def fragen(self, antwort):
sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n")
time.sleep(3)
if antwort.split()[3] == ":blau":
sock.sendall("PRIVMSG #testblablub RISCHTISCH \r\n")
ausfuehren = funktion()
ausfuehren.main()
(sry some strings are written in german, but I think that's not important)
So my main problem is that I want the function def fragen(self, antwort)
to be run in the def haupt_schleife(self)
function (or method for python's sake). All of the important stuff is in this def haupt_schleife(self)
. I want that the rest of the quiz-code to be running in the elif-block of the def haupt_schleife(self)
, so that it's a complete seperate thing. The stupid thing is, if I type in "quiz.start" the : sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n")
is running with no problems, but it doesn't start the if-statement. It just does nothing else in the channel (just prints "Welche Farbe hat der Himmel ? out).
I hope that finaly someone understood this :/ (if you want to run the code you have to type in two names in the command line, because of argv)