파이썬으로 ip주소를 별표(asterisk)로 변환해보자

업무 중 DB에서 뽑은 IP 내역들을 별표로 치환할 일이 있었습니다. 그때 간단하게 파이썬을 이용해서 코딩했습니다. 고객의 IP를 공개 할 수 없으므로 ip주소는 랜덤으로 생성했습니다. 블로그에 100개를 넣기엔 너무 많으니 10개만 첨부합니다.

ip.txt

1
2
3
4
5
6
7
8
9
10
97.198.15.99
169.252.144.58
93.197.54.27
1.51.221.105
186.186.248.151
145.233.42.23
33.93.200.29
140.61.99.198
217.93.185.190
177.253.120.25
cs


ip_asterisk.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
30
31
32
def ip_asterisk(ip):
    (a, b, c ,d) = ip.split('.'# .을 기준으로 a, b, c, d로 나눠서 변수에 할당된다. ex)123.456.789.000 a=123 b=456 c=789 d=000
 
    if len(c) == 1:
        c="*"
    elif len(c) == 2:
        c = "**"
    else:
        c="***"
 
    if len(d) == 1:
        d = "*"
    elif len(d) == 2:
        d = "**"
    else:
        d = "***"
 
    ip_result = a+"."+b+"."+c+"."++ "\n"
    return ip_result
 
file_in = open("ip.txt"'r')
file_out = open("ip_result.txt"'w')
 
while True:
    line = file_in.readline()
    if not line: break
    ip_result = ip_asterisk(line)
    print(ip_result)
    file_out.write(ip_result)
    
file_in.close()
file_out.close()
cs

ip_asterisk.py를 실행해봅시다.
ip.txt와 ip_asterisk.py는 같은 경로 상에 있어야 합니다.
완료시 ip.result.txt가 생성됩니다.

댓글 쓰기

0 댓글