Thread: Python And IRC

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13

    Python And IRC


    I read this article from here:

    http://www.devshed.com/c/a/Python/Python-and-IRC/

    For some reason, I can't get the bot to connect to an irc channel. The syntax is exactly the same as the examples.

    Is it possible that because my router does not accept pings it won't connect? Any way to debug it? Anybody get that example to work?

    Help me obi-wan kenobi...your my only hope.
    I'll learn this stuff someday.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    post your code and the error you're getting
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    No errors, it simply does not connect to the channel. I was trying the code from the article I linked to. It is this one:

    Code:
        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
    I'll learn this stuff someday.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    When running this script I actually get a Traceback:

    Code:
    Traceback (most recent call last):
      File "./test.py", line 8, in ?
        irc.connect ( ( network, port ) )
      File "<string>", line 1, in connect
    socket.gaierror: (7, 'No address associated with nodename')
    Probably because the IRC chanel your trying to connect to is "irc.insert.a.network.here". Try another IRC network. I tried a few and It just seemed to hang however this might not be because if hasn't connected.

    I'll give the IRC article a read though and let you know if anything jumps out.

    Hope this helps,

    Mark,
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Yeah sorry, I actually did replace everything with a correct irc network and channel. Just like you, it hangs and does not connect.

    Thanks.
    I'll learn this stuff someday.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey Shiner,

    I concluded that it does connect by adding a few print statements, mainly inside the PING if block which showed the client and server talking.

    Try connecting with an IRC client and see if you see your bot in the room. If you do, send it a few private messages . I don't use IRC so don't know any IRC network to test this with but it seems like it should work.

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Well, the bot is not connecting to the channel at all. I did stumble upon some code you wrote in another thread:

    Code:
    #!/usr/bin/env python
    
    import socket
    
    class IRC:
    
    	def __init__(self):
    		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    	def connect(self):
    		try:
    			self.sock.connect((self.host, self.port))
    			self.sock.send('NICK %s\n\r' % self.nick)
    			self.sock.send('USER localhost localhost localhost : Python Powered\n\r')
    			self.sock.send('JOIN %s\n\r' % self.room)
    			self.sock.send('PRIVMSG %s : Python Powered\n\r' % self.room)
    			self.sock.send('NAMES\n\r')
    
    			while True:
    				print self.recieve()
    
    		except:
    			print 'Error Occured'
    		
    	def recieve(self):
    		line = self.sock.recv(self.byte)
    
    		if line.startswith('PING'):
    			self.sock.send('PONG %s\n\r' % line)
    			print line
    			return 'PONG has been sent'
    		else:
    			return line
    
    if __name__ == '__main__':
    
    	pybot = IRC()
    	pybot.host = 'irc.us.gamesurge.net'
    	pybot.room = '#channel'
    	pybot.nick = 'CheckMan'
    	pybot.port = 6667
    	pybot.byte = 1025
    	pybot.connect()
    I did have to change the self.sock.send line in order for it to connect. You can see the changes above.

    Now, this code does connect to irc.us.gamesurge.net but it does not connect to the channel(I do have a real channel in the code I'm using)

    What happens is once it connects to irc.gamesurge.net it spits out a bunch of IRC stuff(i.e. motd, server rules, etc.) and then has three lines that end with 'Unknown Command'

    Any idea what 3 commands in the script you made are 'Unkown"?
    I'll learn this stuff someday.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    (just a thought): change the \n\r to \r\n...that *MAY* help...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13

    Unhappy


    I changed them but got the same thing.
    I'll learn this stuff someday.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    Maybe you could add a time.sleep in between the NICK command and the other commands, giving it a few seconds to send the MOTD, etc.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0
    Hello,

    Shiner, the network you're trying to connect to doesn't recognize the script's registration until it's too late. However, we can easily work around that:

    Code:
    import socket
    
    network = 'irc.us.gamesurge.net'
    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 Peyton PyIRC PyIRC :Python IRC\r\n' )
    channelSwitch = 0
    while True:
       data = irc.recv ( 4096 )
       if channelSwitch == 0:
          irc.send ( 'JOIN #pyirc\r\n' )
       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
       elif data.find ( '#pyirc' ) != -1:
          channelSwitch = 1
    -Peyton
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Wow thanks a lot! It actually works now. Believe it or not I actually used the Twisted irc framework to get my bot working but now I can dump that and re-write my program with this.

    Thanks!
    I'll learn this stuff someday.

IMN logo majestic logo threadwatch logo seochat tools logo