Connecting to IRC using socket module
Erik Heneryd
erik at heneryd.com
Fri Oct 22 22:46:54 EDT 2004
Dag Hansteen wrote:
> Hello!
>
> Somehow I dont make this work, can anyone please
> correct me?
Short answer: IRC expects messages to be terminated with "\r\n".
Some additional comments on your code below.
>
> There is no error, but it just kinda halts after I get "NOTICE AUTH: ***
> Found your hostname", I assume theres something with the PONG reply, but
> I can't figure it out.
>
>
> # CODE:
>
> from socket import *
>
> host = "oslo.no.eu.undernet.org"
> port = 6667
> buf = 1024
> addr = (host, port)
>
> first = 'NICK dagan'
> second = 'USER dag "" "oslo.no.eu.undernet.org" :python bot'
IRC uses "\r\n" as message terminator, you forgot those.
Also note that you're not supposed to "-quote message parameters. Not
that it matters here since USER params 2 and 3 are ignored anyway - the
ircd knows what hostname it has and will do a DNS lookup to determine
yours. Furthermore, RFC1459 and RFC2812 don't even agree on what USER
param 2 and 3 mean. I'd say you can safely use * (asterisk) for both of
them.
>
> sock = socket(AF_INET, SOCK_STREAM)
>
>
> sock.connect(addr)
> sock.sendto(first, addr)
> print "-> "+first
Why do you use sendto on a TCP socket? Use send.
>
> sock.sendto(second, addr)
> print "-> "+second
>
>
> while 1:
> data, addr = sock.recvfrom(buf)
Why do you use recvfrom on a TCP socket? Use recv.
Be aware that you might get two or more messages in one go...
>
> f = open("debug.txt", "w")
> f.write(data+"\n")
>
> if data.startswith("PING :"):
Be aware that the param might not be :-quoted...
> sock.send("PONG :"+data[6:])
> break
>
> else:
> print "<- "+data
> print addr
>
> print "I hope I soon will see this message.. yes, this message! woohoo"
>
> sock.send("join #scripting")
It's JOIN, not join.
>
> while 1:
> data, addr = sock.recvfrom(buf)
> print "<- "+data
>
Erik
More information about the Python-list
mailing list