[Tutor] IRC Client Trouble -- too many new lines

Joseph Quigley cpu.crazy at gmail.com
Sun Sep 18 16:35:10 CEST 2005

My IRC Client can't print the IRC server messages without a newline for 
each message in a giant list that I recieve...
here's my code:

import socket, string, sys

#some user data, change as per your taste
class Data:
    SERVER = 'irc.freenode.net'
    CHANNEL = "#python"
    NICKNAME = "Pythonatic"
    PORT = 6667
    #open a socket to handle the connection
    IRC = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#open a connection with the server
def Irc_Conn():
    print "Connecting to IRC server (%s)." % (Data.SERVER)
        Data.IRC.connect((Data.SERVER, Data.PORT))
    except socket.gaierror:
        print """Error: Name or service not known.

This could mean that you aren't connected to the internet or that
the server is down."""
        raise SystemExit

#simple function to send data through the socket
def Send_Data(command):
    Data.IRC.send(command + '\n')

#join the channel
def Join(channel):
    Send_Data("JOIN %s" % channel)

#send login data (customizable)
def Login(nickname, username='user', password = None, realname='You 
don\'t need2know', hostname='Slackware', servername='Intelnett'):
    Send_Data("USER %s %s %s %s" % (username, hostname, servername, 
    Send_Data("NICK " + NICKNAME)


while (1):
    buffer = Data.IRC.recv(1024)
    msg = string.split(buffer)
    if msg[0] == "PING": #check if server have sent ping command
        Send_Data("PONG %s" % msg[1]) #answer with pong as per RFC 1459
    if msg[1] == 'PRIVMSG' and msg[2] == Data.NICKNAME:
        filetxt = open('%stmp' % (Data.configFileRead[8]), 'a+') #open 
an arbitrary file to store the messages
        nick_name = msg[0][:string.find(msg[0],"!")] #if a private 
message is sent to you catch it
        message = ' '.join(msg[3:])
        filetxt.write(string.lstrip(nick_name, ':') + ' -> ' + 
string.lstrip(message, ':') + '\n') #write to the file
        filetxt.flush() #don't wait for next message, write it now!
    if msg:
        msg_length = len(msg)
        print_msg = 0
        while True:
            if print_msg == msg_length:
                print msg[print_msg]
                print_msg = print_msg + 1


More information about the Tutor mailing list