Python
  Home arrow Python arrow Page 4 - Python and IRC
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python and IRC
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 29
    2005-02-16

    Table of Contents:
  • Python and IRC
  • Hello IRC World
  • Can You Hear Me?
  • I Can Hear You!
  • IRC Mathematics

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python and IRC - I Can Hear You!
    (Page 4 of 5 )

    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


       · Hey, all. I thought this would be a unique topic to write about. I haven't seen...
       · very nice article, it's very rare to see someone talking about writing scripts for...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway