In order to learn some basics about sockets I'm trying to program a simple IRC client. In order to receive server messages I have a thread that is receiving from the socket. When sending commands to the server a new thread is created, with the same socket. This doesn't seem to work. The commands are executed, but do not seem to be sent through the socket (nothing happens).
So, my question is this. Is this approach, with threads and a socket, impossible to realise? An IRC client needs to have only one persistent connection, right? Can I get any feedback from the socket.send, when sending commands, to get a clue of whats happening? I'm attaching the present code if anyone wants to see it.
# Echo client program import socket, threading from time import gmtime, strftime HOST = '22.214.171.124' # The remote host PORT = 6667 # The same port as used by the server NICK = "hadoque" # Irc nickname IDENT = "hadoque" # Irc Ident REALNAME = "Johan" # Irc real name lock = threading.Lock() #***class Parser *** #Takes a socket and a command, sends a formatted command to server class Parser(threading.Thread): def run(self): pass def __init__(self, socket, command): threading.Thread.__init__(self) self.socket = socket self.command = command.split() global HOST global PORT global NICK global IDENT global REALNAME #function sendToServer #Takes a string to be sent, sends it and prints the sent command with a time stamp def sendToServer(self, snd): self.socket.send(snd) print ">>", strftime("%H:%M:%S", gmtime()), snd #function serverParser #handles server pings by sending back a PONG def serverParser(self, buf): buf = buf.split() if buf.lower() == "ping": self.sendToServer("PONG %s" % buf) #Checks for commands and sends the formatted version of the command to the server def run(self): # print "\033[95m", strftime("%H:%M:%S", gmtime()), threading.currentThread(), "\033[0m" #QUIT command if self.command.lower() == "quit": self.sendToServer("QUIT") #JOIN #channel command if self.command.lower() == "join": self.sendToServer("JOIN %s" % self.command) #connects to server by socket and sends login commands to server #after this it listens for server messages and pings if self.command.lower() == "connect": self.socket.connect((HOST, PORT)) print "Connected to", HOST, "port", PORT self.sendToServer("NICK %s\r\n" % NICK) self.sendToServer("USER %s %s bla :%s \r\n" % (IDENT, HOST, REALNAME)) dataBuffer = "" #This loop receives data from socket and looks for "\r\n", since every server message #is ended with this. When finding a "\r\n" it cuts the received data at that point #and prints to screen with a time stamp. while True: data = self.socket.recv(512) dataBuffer += data while "\r\n" in dataBuffer: part_data = dataBuffer.partition("\r\n") dataBuffer = part_data print strftime("%H:%M:%S", gmtime()), dataBuffer self.serverParser(dataBuffer) dataBuffer = part_data s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Waits for user input. Starts a new thread for every command passed while True: cmd = raw_input(">>") Parser(s, cmd).start()
- 1 Contributor
- forum1 Reply
- 2 Views
- 7 Years Discussion Span
- comment Latest Post by hadoque