7.11.2013

How to Send an IRC Message with SSL in Python

Python: SSL 経由で IRC メッセージを送信する方法

  • IRC ライブラリをインストール
    irc 8.3.1 : Python Package Index

    $ sudo easy_install irc

    または

    $ sudo pip install irc
  • 実装例
    この辺りを参考に
    jaraco / irc / source / scripts / ssl-cat.py — Bitbucket
    irc_message.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # -*- coding: utf-8 -*-
     
    import ssl
     
    SERVER_ADDRESS = 'xxx.xxx.xxx.xxx'
    SERVER_PORT = 6667
    SERVER_PASS = 'xxxxxx'
     
    LOGIN_NAME = 'xxxxxx'
    LOGIN_CHANNEL = '#xxxxxx'
     
     
    def send(message):
        try:
            import irc.client
            import irc.connection
     
            client = irc.client.IRC()
            server = client.server()
            factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
     
            c = server.connect(
                SERVER_ADDRESS, SERVER_PORT, LOGIN_NAME, SERVER_PASS,
                connect_factory=factory)
     
            c.privmsg(LOGIN_CHANNEL, message)
            c.disconnect()
        except Exception as e:
            print('WARN: Failed to send IRC message. (%s)' % e)

チャンネル宛にプライベートメッセージを送ると、メンバー全員へ通知される。

サーバのパスワードを間違えてもExceptionは発生しない。
どうやって気づけばよいのだろう。

 

Related Posts

mog project: Python: Sending IRC Message, Improved

0 件のコメント:

コメントを投稿