python - 我如何从这里完全删除 colorama?
问题描述
我从一个朋友那里得到了这个代码,但是因为他在 linux 上,所以他有 colorama,它对我不起作用,当颜色代码在那里时,很难阅读和/或复制和粘贴结果。我需要编辑什么所以 colorama 不在其中?
#!/usr/bin/python3
import requests
import json
import sys
import struct
import socket
import colorama
from colorama import Fore
indent = " "
def ip2bin(ip): # Credit to converter
binary = bin(struct.unpack('!I', socket.inet_aton(ip))[0])
return binary
def arg_length():
return len(sys.argv)
def print_help():
print("Commands:")
print("** ipl -i <ip> - Gets information from an IP address")
print("** ipl -h - Displays this page.")
def lookup(ip):
print("Looking up IP address \"" + str(ip) + "\"..")
re = requests.get("https://ipinfo.io/" + str(ip) + "/json")
j = json.loads(re.content)
postal = ''
loc = ''
hostname = ''
print("IP Information:")
for key, value in j.items():
maininfo = True
if str(key) == "error":
print("Error: Invalid IP address")
exit(0)
if str(key) != "readme":
if key == "postal":
postal = value
maininfo = False
if key == "loc":
loc = value
maininfo = False
if key == 'hostname':
loc = value
maininfo = False
if maininfo:
print(indent + key, "-", Fore.BLUE + value + Fore.WHITE)
binary = ip2bin(ip).replace('0b', '')
ipClass = 'Class '
if binary.startswith("0"):
ipClass += 'A'
elif binary.startswith('10'):
ipClass += 'B'
elif binary.startswith('110'):
ipClass += 'C'
elif binary.startswith('1110'):
ipClass += 'D'
elif binary.startswith('1111'):
ipClass += 'E'
print("Additional Information:")
print(indent + "IP Class: " + Fore.BLUE + ipClass + Fore.WHITE)
print(indent + "Postal Code: " + Fore.BLUE + postal + Fore.WHITE)
print(indent + "Long/Lat: " + Fore.BLUE + loc + Fore.WHITE)
if not hostname == '':
print("Hostname: " + hostname)
if arg_length() == 1 or arg_length() == 2:
print_help()
if arg_length() == 3:
if str(sys.argv[1]) == "-i":
lookup(str(sys.argv[2]))
else:
print_help()
解决方案
只需删除 colorama 和 Fore 的所有用法,如下所示:
#!/usr/bin/python3
import requests
import json
import sys
import struct
import socket
indent = " "
def ip2bin(ip): # Credit to converter
binary = bin(struct.unpack('!I', socket.inet_aton(ip))[0])
return binary
def arg_length():
return len(sys.argv)
def print_help():
print("Commands:")
print("** ipl -i <ip> - Gets information from an IP address")
print("** ipl -h - Displays this page.")
def lookup(ip):
print("Looking up IP address \"" + str(ip) + "\"..")
re = requests.get("https://ipinfo.io/" + str(ip) + "/json")
j = json.loads(re.content)
postal = ''
loc = ''
hostname = ''
print("IP Information:")
for key, value in j.items():
maininfo = True
if str(key) == "error":
print("Error: Invalid IP address")
exit(0)
if str(key) != "readme":
if key == "postal":
postal = value
maininfo = False
if key == "loc":
loc = value
maininfo = False
if key == 'hostname':
loc = value
maininfo = False
if maininfo:
print(indent + key, "-", value)
binary = ip2bin(ip).replace('0b', '')
ipClass = 'Class '
if binary.startswith("0"):
ipClass += 'A'
elif binary.startswith('10'):
ipClass += 'B'
elif binary.startswith('110'):
ipClass += 'C'
elif binary.startswith('1110'):
ipClass += 'D'
elif binary.startswith('1111'):
ipClass += 'E'
print("Additional Information:")
print(indent + "IP Class: " + ipClass)
print(indent + "Postal Code: " + postal)
print(indent + "Long/Lat: " + loc)
if not hostname == '':
print("Hostname: " + hostname)
if arg_length() == 1 or arg_length() == 2:
print_help()
if arg_length() == 3:
if str(sys.argv[1]) == "-i":
lookup(str(sys.argv[2]))
else:
print_help()
我认为人们投票反对你是因为你表现出缺乏努力。但是,我花了 20 秒来解决您的问题,所以我不介意回答。
最好只使用我的答案,然后删除您的问题。
推荐阅读
- python - 如何修复“无效的密码格式或未知的哈希算法”。在自定义用户模型 Django
- jenkins - 如何在 jenkins 管道脚本中集成 Service Now?
- dynamic-programming - 位图模式识别
- javascript - Angular 4,错误:无法解析 StateObservable 的所有参数:(?)
- javascript - 节点集成在 Electron 中不起作用?
- testing - Ansible wait_for 不响应正确错误
- javascript - 从第 3 方代码访问主机页面的 googletag 广告
- javascript - 是否可以通过 Vue.js 应用程序更改 Firefox 浏览器设置?
- reactjs - ReactJS:重置选择内占位符的显示
- git - 创建从 branch1 到 branch2 的拉取请求