Created
October 19, 2016 07:27
-
-
Save jxinging/9de42f1060a015b5c8a7b50899110995 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# coding: utf8 | |
""" """ | |
import sys | |
import re | |
import os | |
from ipip import IP | |
IP.load(os.path.join('./data/17monipdb.dat')) | |
IPADDR_RE = re.compile(r'\b(?:[\d]{1,3}\.){3}[\d]{1,3}\b') | |
def replace_func(match): | |
orig = match.group() | |
replace = "%s[%s]" % (orig, " ".join(IP.find(orig).strip().split())) | |
return replace | |
def main(): | |
ip_char_set = set("0123456789.") | |
while 1: | |
line = [] | |
c = sys.stdin.read(1) | |
while c in ip_char_set: | |
line.append(c) | |
c = sys.stdin.read(1) | |
line.append(c) | |
if len(line) >= 7 and "." in line: | |
sys.stdout.write(IPADDR_RE.sub(replace_func, "".join(line))) | |
else: | |
sys.stdout.write("".join(line)) | |
sys.stdout.flush() | |
if not c: | |
break | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment