raw_input() with out blocking process

TheDustbustr thedustbustr at aol.com
Tue Jul 24 21:15:12 EDT 2001


I am writing an IRC client, and have not found a good solution to a major
problem: How do I enter commands to be sent while not blocking the process (so
that the client continues to recieve and print data to the screen)? 
raw_input() waits for you to press enter, and blocks the program.  I tried
using threads for the raw_input() but this didnt work because whenever text was
printed the raw_input() was terminated or something (it just didnt work at
all).  My best solution was to use threads and ANOTHER program- a server to
type commands into (with raw_input() ) that uses sockets to communicate with
the IRC client (the command reciever subroutine a thread so s.accept() doesn't
block the process).  The IRC client recieves the data from the command server
(it prints anything it recieves) but doesn't send it along (not sure why,
theres a bug in my code somewhere).  Is there a better way to issue commands to
my client, or do I have to find the bug?  I'll post the code below so you can
graciously help ;)  I think the problem is that you cannot send data to the IRC
server from the subroutine, but I'm not sure how to do that... it has to be a
subroutine because its a thread...

ANY help at all is appreciated =)
Thanks a million, Dustin

# BEGIN CODE BLOCK 1
# irc.py
import socket, string, sys, errno, thread
HOST = 'irc.box.sk'
PORT = 6667
NICK='Dustin[bot]'
REAL='MyReal Name'
QUITMSG='Bye'
IDENT='hacker'

def parse(data):     #print data to screen in orderly fashion
    if string.find(string.lower(data), 'privmsg') >= 0:
        if data[:1]==':':     #was recieved from IRC server because it was
routed
            garbage,middle,msg = string.split(data,':')
            sender,cmd,rcpt = string.split(middle)
            return '<%s> %s' % (sender,msg)
        else:                   #was recieved from control program
            middle,msg = string.split(data,':')
            cmd,rcpt = string.split(middle)
            return '<%s> %s' % (NICK,msg)
    else: 
        return data

def get_control():     #recieve data from control program
    while 1:
        control = ctl.recv(256)
        if string.lower(control) == 'die':
            irc.send('QUIT :%s' % QUITMSG)     #quit IRC
            ctl.close     #kill all the sockets
            irc.close
            break     #terminate loop therefore exiting the thread
        else:
            irc.send(control)
            print control

#set up connection to IRC server
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((HOST, PORT))
log=open('irclog.txt','w')     #set up log
print 'Connecting to %s:%s' % (HOST, PORT)

while 1:	# initial registration
    data = irc.recv(1024)
    log.write(data)
    print data
    if string.find(string.lower(data), 'got ident response'): 
        break     #the connection was established

#register on the IRC server, send USER command followed by NICK
irc.send('USER %s localhost localhost :%s\n' % (IDENT, REAL))
irc.send('NICK %s\n' % NICK)

# connect to control server, a program ALREADY RUNNING:
ctl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ctl.connect(('localhost', 5989))

# start recieving commands from control program
thread.start_new_thread(get_control,())

while 1:	# main program loop
    data = irc.recv(2048)     #recieve data from IRC server
    log.write(data)
    print data
    #print parse(data)

# END CODE BLOCK 1

# BEGIN CODE BLOCK 2
# ircctl.py
# This is a SERVER running on localhost, it sends any inputted
# text to the main IRC client through localhost:5989
import socket, string

#set up connection,
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 5989))
s.listen(1)

#conveniently blocks process so you cant prematurely send data
conn, addr = s.accept()
while 1:
    cmd = raw_input(">")
    conn.send(cmd)
    if cmd=='die':
        conn.close
        break
#END CODE BLOCK 2



More information about the Python-list mailing list