views:

463

answers:

6

Hello,

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.

+1  A: 

You already have the blueprint there for what you want it to do. You're doing:

if(line[0]=="PING"):

No reason you couldn't adapt that scheme to accept input of PORT, NICK, etc.

Also, while 1 isn't very Pythonic. Yes it works, but really there is no reason not to use True. It's not a big deal, but it makes the code slightly more readable.

AlbertoPL
A: 

Hello AlbertoPL,

But how i can make this, because i'm starting in Python development. Thanks!

Nathan Campos
+1  A: 

Not a direct answer, but you should check the IRC implementation in twisted, an event-driven networking engine written in Python that includes support for irc in twisted.words.protocols.irc.

gimel
That's a good library!
Nathan Campos
A: 

If you're brand new to Python, an IRC client is quite an undertaking, especially if you haven't worked with similar clients before in other languages.

I would recommend you to look up on threading, so that you can put your IRC handler on a separate thread, and receive user input on another thread (If you do both on the same thread, one will block the other, making for a bad experience.)

To answer your question though, the simplest way to get input from the user in the console is to use in = raw_input(), but as I said, it will not interact well with the socket on the same thread.

Blixt
+2  A: 

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
>>>
Hawker
Thanks very much!!!
Nathan Campos
+1  A: 

If you're trying to carry out actions in response to user input, maybe the cmd module will help you out:

If you're interested in the IRC protocol itself, this tutorial on using sockets to write an IRC client in python may be of use:

ars
Thanks for the base!
Nathan Campos