IP2LOC
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IP2LOC相关的知识,希望对你有一定的参考价值。
demo.txt 127.0.0.1:80 127.0.0.2 127.0.0.3:443 127.0.0.4 127.0.0.5:8080
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: f0rsaken import IP import sys def main(): try: file = sys.argv[1] except IndexError as e: print(e) sys.exit(1) try: with open(file, "r") as f: temp = f.readlines() except FileNotFoundError as e: print(e) sys.exit(1) ip_list = list() for t in temp: ip_list.append(t.strip()) del temp chinese_mainland = list() not_chinese_mainland = list() for ip in ip_list: if IP.find(ip.split(":")[0]).split("\t")[0] == "中国" and IP.find(ip.split(":")[0]).split("\t")[1] not in ("香港", "澳门", "台湾"): chinese_mainland.append(ip + "\t" + IP.find(ip.split(":")[0]) + "\n") # chinese_mainland.append(ip + "\n") else: not_chinese_mainland.append(ip + "\t" + IP.find(ip.split(":")[0]) + "\n") # not_chinese_mainland.append(ip + "\n") del ip_list with open("chinese_mainland.txt", "w") as f: f.writelines(chinese_mainland) with open("not_chinese_mainland.txt", "w") as f: f.writelines(not_chinese_mainland) if __name__ == "__main__": main()
以上是关于IP2LOC的主要内容,如果未能解决你的问题,请参考以下文章