Home arrow Python arrow Page 4 - Python and IRC

I Can Hear You! - Python

IRC is becoming an increasingly popular medium for communication. In this article, Peyton McCullough explains how to make Python and IRC work together.

TABLE OF CONTENTS:
  1. Python and IRC
  2. Hello IRC World
  3. Can You Hear Me?
  4. I Can Hear You!
  5. IRC Mathematics
By: Peyton McCullough
Rating: starstarstarstarstar / 34
February 16, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

So far, we can connect to an IRC network and send a message to a given channel. We can also send a message to another user. Now we will accept messages from both the server and from other users.

Every once in a while, the server will send us a "PING" command to check on us. To stay connected, we must send the server a "PONG" command. Let's build a script that does just that.

import socket

network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'NICK PyIRC\r\n' )
irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
irc.send ( 'JOIN #pyirc\r\n' )
irc.send ( 'PRIVMSG #pyirc :Hello.\r\n' )
while True:
   data = irc.recv ( 4096 )
   if data.find ( 'PING' ) != -1:
      irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
   print data

As you can see, we've changed our skeleton a bit in this script. We have replaced the bottom section with an infinite loop. The loop receives data, and if a "PING" command is present, it replies with a "PONG" command. Feel free to test the script out.

Let's modify our script to accept messages. Messages come in a form similar to this:

:Nick!user@host PRIVMSG destination :Message

Here's an example:

:Peyton!~peyton@python.org PRIVMSG #pyrc :Hey!

In our new script, we will break down the above form of data.

import socket

network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
trash = irc.recv ( 4096 )
irc.send ( 'NICK PyIRC\r\n' )
irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
irc.send ( 'JOIN #pyirc\r\n' )
irc.send ( 'PRIVMSG #pyirc :Hello.\r\n' )
while True:
   data = irc.recv ( 4096 )
   if data.find ( 'PING' ) != -1:
      irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
   elif data.find ( 'PRIVMSG' ) != -1:
      nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
      message = ':'.join ( data.split ( ':' ) [ 2: ] )
      print nick + ':', message

We've added a few lines to the script. If the "PRIVMSG" command is found inside the line of data, we pull the line apart to get the nickname of the person who sent it and the message by using the split() function. Run the script, join #pyirc on the specified network and test out the script.

Now we need our script to discriminate between messages in different channels and private messages. This can be done easily by extracting the destination from the "PRIVMSG" command.

import socket

network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
irc.recv ( 4096 )
irc.send ( 'NICK PyIRC\r\n' )
irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
irc.send ( 'JOIN #pyirc\r\n' )
irc.send ( 'PRIVMSG #pyirc :Hello.\r\n' )
while True:
   data = irc.recv ( 4096 )
   if data.find ( 'PING' ) != -1:
      irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
   elif data.find ( 'PRIVMSG' ) != -1:
      nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
      message = ':'.join ( data.split ( ':' ) [ 2: ] )
      destination = ''.join ( data.split ( ':' ) [ :2 ] ).split ( ' ' ) [ -2 ]
      if destination == 'PyIRC':
         destination = 'PRIVATE'
      print '(', destination, ')', nick + ':', message

Test out the script like before and see the result. You should see something similar to this:

( #pyirc ) Peyton: Test

( PRIVATE ) Peyton: This is a private message.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: