-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·65 lines (58 loc) · 3.08 KB
/
Copy pathmain.py
File metadata and controls
executable file
·65 lines (58 loc) · 3.08 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
################################################################################################################################################
# ShortName: binary.py
# FullName: binary_converter.py
# Author: ArenGamerZ
# Email: arendevel@gmail.com
# Version: 3.2.2-stable
# Description: This is a program that will convert given IP address or a number into its binary representation and viceversa
# License GNU GPL, check out the full notice in LICENSE file
# Copyright (C) 2017 ArenGamerZ
################################################################################################################################################
import argparse, sys
from modules import colors as c
from modules import bin_converter as converter
def loop(times, format="decimal"):
""" This function loops to enter several IPs
Arguments:
times = The number of times program will loop
format = The format of the number given: 'decimal' or 'binary'"""
try:
for i in range(times):
ip = input(c.fcolors.GREY+"IP or number: "+c.fcolors.YELLOW)
if format == "decimal":
print(c.fcolors.GREY+"Binary representation: "+c.fcolors.YELLOW+converter.convert(ip)+c.fcolors.RESET)
elif format == "binary":
print(c.fcolors.GREY+"Decimal representation: "+c.fcolors.YELLOW+converter.convert(ip, "binary"))
print("")
except KeyboardInterrupt:
print("")
print(c.fcolors.RED+"\nAborted by user"+c.fcolors.RESET)
sys.exit(0)
if __name__ != "__main__":
print(c.fcolors.RED+"This module is not meant to be imported!!!"+c.fcolors.RESET)
else:
parser = argparse.ArgumentParser(description="This is a program that will convert given IP address or a number into its binary representation and viceversa")
conflict = parser.add_mutually_exclusive_group()
conflict.add_argument("IP", help="IP/number to convert", type=str, nargs='?')
parser.add_argument("-b", "--binary", action="store_true", help="Flag to specify binary format")
conflict.add_argument("-l", "--loop", dest="times", type=int, help="(-l)oops <TIMES> times")
args = parser.parse_args()
try:
if sys.argv[1]:
if args.IP:
if args.binary and not args.times:
print(c.fcolors.GREY+"Decimal representation: "+c.fcolors.YELLOW+converter.convert(args.IP, "binary")+c.fcolors.RESET)
else:
print(c.fcolors.GREY+"Binary representation: "+c.fcolors.YELLOW+converter.convert(args.IP)+c.fcolors.RESET)
else:
if args.binary and args.times:
loop(args.times, "binary")
elif args.times:
loop(args.times)
else:
print(c.fcolors.RED+"'-b', '--binary' It's a flag, it does nothing itself"+c.fcolors.RESET)
except IndexError:
print(c.fcolors.RED+"No arguments given! Use '-h' to see the help"+c.fcolors.RESET)
except ValueError:
print(c.fcolors.RED+"That was not a valid IP Address nor a number!")