Home arrow Python arrow Page 2 - IRC on a Higher Level

Getting Started - Python

Python is very suitable for working with the Internet Relay Chat (IRC) protocol. But working directly with a protocol can be a bit messy. Fortunately, there is a library that can simplify this work: Python-IRCLib. This article, the first of three parts, takes a look at this library and what you can do with it, with a focus on event handling.

  1. IRC on a Higher Level
  2. Getting Started
  3. Events
  4. All Supported Events
By: Peyton McCullough
Rating: starstarstarstarstar / 10
October 05, 2005

print this article



Before you start coding, join the network “irc.freenode.net” and enter the channel “#irclib” on your IRC client of preference. We'll do all of our testing there. If you wish, though, you are free to use any channel on any network. However, it is important that you do not test the code in this article out in a populated channel. Users will quickly get annoyed.

Now it's time to jump into some code. First, we'll try joining an IRC server, sending it a message and leaving. Python-IRCLib makes this quite easy:

import irclib

# Connection information
network = 'irc.freenode.net'
port = 6667
channel = '#irclib'
nick = 'PyTest'
name = 'Python Test'

# Create an IRC object
irc = irclib.IRC()

# Create a server object, connect and join the channel
server = irc.server()
server.connect ( network, port, nick, ircname = name )
server.join ( channel )

# Jump into an infinite loop

It may take a moment for your application to join the channel, so don't panic if you have to wait a moment. Notice how much easier it is to use a library rather than work directly with the IRC protocol.

With a single IRC object, it is possible to create multiple server objects, allowing for multiple connections:

import irclib

# Connection information for the first connection
network1 = 'irc.freenode.net'
port1 = 6667
channel1 = '#irclib'
nick1 = 'PyTest1'
name1 = 'Test One'

# Information for the second connection
network2 = 'irc.freenode.net'
port2 = 6667
channel2 = '#irclib'
nick2 = 'PyTest2'
name2 = 'Test Two'

# Create an IRC object
irc = irclib.IRC()

# Make the first connection
server1 = irc.server()
server1.connect ( network1, port1, nick1, ircname = name1 )
server1.join ( channel1 )

# Make the second connection
server2 = irc.server()
server2.connect ( network2, port2, nick2, ircname = name2 )
server2.join ( channel2 )

# Infinite loop

The privmsg method is used to send messages. Recall that “PRIVMSG” is used to send messages to both users and channels:

import irclib

# Set this variable to your nickname
me = 'Peyton'

# Connection information
network = 'irc.freenode.net'
port = 6667
channel = '#irclib'
nick = 'PyTest'
name = 'Test One'

# Connect
irc = irclib.IRC()
server = irc.server()
server.connect ( network, port, nick, ircname = name )
server.join ( channel )

# Message both the channel and you
server.privmsg ( channel, 'PRIVMSG to a channel.' )
server.privmsg ( me, 'PRIVMSG to a user.' )

# Loop

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: