Search code examples
pythonsocketsclientpython-3.xirc

Improve a IRC Client in Python


How i can make some improvement in my IRC client made in Python. The improvement is: How i can put something that the user can type the HOST, PORT, NICK, INDENT and REALNAME strings and the message? And here is the code of the program:

simplebot.py

import sys
import socket
import string

HOST="irc.freenode.net"
PORT=6667
NICK="MauBot"
IDENT="maubot"
REALNAME="MauritsBot"
readbuffer=""

s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))

while 1:
    readbuffer=readbuffer+s.recv(1024)
    temp=string.split(readbuffer, "\n")
    readbuffer=temp.pop( )

    for line in temp:
        line=string.rstrip(line)
        line=string.split(line)

        if(line[0]=="PING"):
            s.send("PONG %s\r\n" % line[1])

Remember that i'm starting in Python development. Here is where i have found this code: http://oreilly.com/pub/h/1968.Thanks.


Solution

  • So you want the user to control the exact connection information that the IRC client uses? In order to do this, you must collect input from the user before you start your connection using the raw_input function.

    NOTE: raw_input will strip a trailing newline character.

    HOST = raw_input('Enter Host: ')
    PORT = int(raw_input('Enter Port: '))
    

    ...for all of the values that you want the user to be able to configure.

    Example:

    HOST = raw_input('Enter host: ')
    print HOST
    
    >>> 
    Enter host: stackoverflow.com
    stackoverflow.com
    >>>