Comment detail

アクセスログのIPアドレスを逆引き (Nested Flatten)

This comment is reply for 1813 ocean: Pythonで組んでみました。最初 th...(アクセスログのIPアドレスを逆引き). Go to thread root.

スレッドの生成コストが気にならなければ、この方がシンプルですね。
 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from __future__ import with_statement
import collections
import threading
import socket
import sys
import re

cache = {} # addr: host
cond = threading.Condition() # protect `cache'

class Resolver(threading.Thread):
    def __init__(self, addr):
        threading.Thread.__init__(self)
        self._addr = addr

    def run(self):
        try:
            host = socket.gethostbyaddr(self._addr)[0]
        except socket.error:
            host = self._addr
        with cond:
            cache[self._addr] = host
            cond.notify()

def main():
    queue = collections.deque()
    def output(count):
        while len(queue) > count:
            addr, rest = queue.pop()
            with cond:
                while cache[addr] is None:
                    cond.wait()
                sys.stdout.write("%s%s" % (cache[addr], rest))
    for line in open("access.log"):
        m = re.search('^' + '\.'.join(['\d+'] * 4), line)
        if not m:
            raise ValueError("line should start with IP address")
        addr, rest = m.group(0), line[m.end(0):]
        queue.appendleft((addr, rest))
        with cond:
            if addr not in cache:
                cache[addr] = None
                thread = Resolver(addr)
                thread.start()
        output(10) # configure this count!
    output(0)

if __name__ == '__main__':
    main()

Index

Feed

Other

Link

Pathtraq

loading...