3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonで簡単なIRCクライアント

Last updated at Posted at 2016-11-17

#IRCクライアント
pythonで簡単なIRCクライアントかきました。

irc.py
#!/usr/bin/env python

import sys, socket, os


TARGET = "localhost"
PORT = 6667

BUF_SIZE = 1024


def irc_connect(irc_socket, target, port):
    irc_socket.connect((target, port))


def login(irc_server, nickname, username, realname, hostname = "hostname", servername = "*"):
    nick_message = "NICK " + nickname + "\n"
    user_message = "USER %s %s %s :%s\n" % (username, hostname, servername, realname)


    irc_server.send(nick_message)
    irc_server.send(user_message)


def join(irc_server, channel):
    join_message = "JOIN " + channel + "\n"

    irc_server.send(join_message)


def pong(irc_server, daemon, daemon2 = None):
    pong_message = "PONG %s %s" % (daemon, daemon2)
    pong_message += "\n"

    irc_server.send(pong_message)


def privmsg(irc_server, channel, text):
    privmsg_message = "PRIVMSG %s :%s\n" % (channel, text)

    irc_server.send(privmsg_message)


def quit(irc_server):
    None


def handle_privmsg(prefix, receiver, text):
    print ""
    print prefix + ">" + text
    print ""


def wait_message(irc_server):
    while(True):
        msg_buf = irc_server.recv(BUF_SIZE)
        msg_buf = msg_buf.strip()

        prefix = None
        if msg_buf[0] == ":":
            p = msg_buf.find(" ")
            prefix = msg_buf[1:p]
            msg_buf = msg_buf[(p + 1):]

        p = msg_buf.find(":")
        if p != -1:#has last param which starts with ":"
            last_param = msg_buf[(p + 1):]
            msg_buf = msg_buf[:p]
            msg_buf = msg_buf.strip()

        messages = msg_buf.split()

        command = messages[0]
        params = messages[1:]

        if command == "PING":
            pong(irc_server, params[0])
        elif command == "PRIVMSG":
            text = last_param
            receiver = ""

            for param in params:
                receiver = param


            handle_privmsg(prefix, receiver, text)


def client_interface(irc_server, channel, prompt = ">"):
    while(True):
        print prompt,
        line = raw_input()

        if line == "quit":
            quit(irc_server)
            sys.exit(0)

        privmsg(irc_server, channel, line)


def main():
    nickname = "nickhoge"
    username = "usr"
    realname = "realname"
    channel = "#test_channel"

    irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    irc_connect(irc, TARGET, PORT)

    login(irc, nickname, username, realname)

    join(irc, channel)

    pid = os.fork()

    if(pid == 0):#child
        wait_message(irc)
    else:
        client_interface(irc, channel)


if __name__ == "__main__":
    main()

#参考
https://tools.ietf.org/html/rfc1459#section-4.4.1
http://www.codereading.com/codereading/python/python-irc-client.html

3
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?