Hi there guys and girls.
I'm writing an IRC bot (even though there are loads out there that can do what I want, I love to learn-by-doing) and currently use the following to connect to the server:
[CODE=python]import sys, string, os
import socket, asyncore, asynchat
from irc_config import *
from irc_parse import *
from irc_modules import *
#from irc_connect import *
# create our socket
sock = socket.socket()
# start us up
parser = parse( sock, config.s )
# load all our modules
startup_modules ( parser )
# connect to our server
sock.connect( ( config.s, config.p ) )
# our buffer destination
readbuffer = ''
# send our initial nick/user bits
sock.send( 'NICK %s\r\n' % config.n )
sock.send( 'USER %s iw 0 :%s\r\n' % ( config.i, config.r ) )
# enter our data loop
while 1:
try:
bytes_received = readbuffer
while bytes_received >= config.b:
packet = sock.recv( config.b )
bytes_received = len( packet )
readbuffer += packet
except Exception, e:
if config.b == 1:
print e
print '<< DEBUG >> Fatal error.'
sys.exit(1)
while readbuffer.find ( '\r\n' ) >= 0:
currentline = readbuffer[:readbuffer.fin d( '\r\n' )]
readbuffer = readbuffer[len( currentline )+2:]
print currentline
parser.irc_pars e( currentline )[/CODE]
Now, this works - just fine and dandy. However, the birds aren't quite tweeting and I'm not sitting bare arsed on the office photocopier with a party hat on quite yet. No, no. In fact, I realise that this blocking method of reading sucks hard. I have written numerous IRC clients in C before that use select and fifo buffers and I wish to do the same in python.
I am having a very hard time getting my head around even where to start with this - can someone please help with a very simple example that I can butcher to my own evil devices?
Thanks for the help in advance,
Ian
I'm writing an IRC bot (even though there are loads out there that can do what I want, I love to learn-by-doing) and currently use the following to connect to the server:
[CODE=python]import sys, string, os
import socket, asyncore, asynchat
from irc_config import *
from irc_parse import *
from irc_modules import *
#from irc_connect import *
# create our socket
sock = socket.socket()
# start us up
parser = parse( sock, config.s )
# load all our modules
startup_modules ( parser )
# connect to our server
sock.connect( ( config.s, config.p ) )
# our buffer destination
readbuffer = ''
# send our initial nick/user bits
sock.send( 'NICK %s\r\n' % config.n )
sock.send( 'USER %s iw 0 :%s\r\n' % ( config.i, config.r ) )
# enter our data loop
while 1:
try:
bytes_received = readbuffer
while bytes_received >= config.b:
packet = sock.recv( config.b )
bytes_received = len( packet )
readbuffer += packet
except Exception, e:
if config.b == 1:
print e
print '<< DEBUG >> Fatal error.'
sys.exit(1)
while readbuffer.find ( '\r\n' ) >= 0:
currentline = readbuffer[:readbuffer.fin d( '\r\n' )]
readbuffer = readbuffer[len( currentline )+2:]
print currentline
parser.irc_pars e( currentline )[/CODE]
Now, this works - just fine and dandy. However, the birds aren't quite tweeting and I'm not sitting bare arsed on the office photocopier with a party hat on quite yet. No, no. In fact, I realise that this blocking method of reading sucks hard. I have written numerous IRC clients in C before that use select and fifo buffers and I wish to do the same in python.
I am having a very hard time getting my head around even where to start with this - can someone please help with a very simple example that I can butcher to my own evil devices?
Thanks for the help in advance,
Ian
Comment