Skip to content

Commit 08be2e1

Browse files
Several updates
- Fixed the issue where the command line will throw unexpected error if the input parameter is not sufficient. - Improved a few messages to be more informative. - Added a ValueError error handling to handle possible error when converting IP number to IP address.
1 parent 25db6ed commit 08be2e1

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

ip2location_csv_converter/commandline.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ def print_usage():
1919
" IP numbers will be converted into hexadecimal format. (auto padding)\n"
2020
"\n"
2121
" -hex4\n"
22-
" IP numbers will be converted into hexadecimal format. (pad IPv4)\n"
22+
" IP numbers will be converted into hexadecimal format. (pad IPv4)\n"
2323
"\n"
2424
" -hex6\n"
25-
" IP numbers will be converted into hexadecimal format. (pad IPv6)\n"
25+
" IP numbers will be converted into hexadecimal format. (pad IPv6)\n"
2626
"\n"
2727
" -replace\n"
2828
" The IP numbers in will be replaced to the selected format.\n"
@@ -35,6 +35,8 @@ def print_usage():
3535
)
3636

3737
def main():
38+
param1 = ''
39+
param2 = ''
3840
if (len(sys.argv) > 2):
3941
# print (sys.argv)
4042
if (len(sys.argv) == 3):
@@ -50,15 +52,16 @@ def main():
5052
input_file = sys.argv[3]
5153
output_file = sys.argv[4]
5254

55+
conversion_mode = 'range'
56+
write_mode = 'replace'
57+
5358
if ((os.path.isfile(input_file)) is False):
5459
print ("File doesn't exist! Please check again.")
5560
sys.exit(1)
5661
else:
5762
if (check_data_validity(input_file) is False):
5863
print ("Please make sure the columns had comma as separator.")
5964
sys.exit(1)
60-
regex1 = r"^\-(range|cidr|hex|hex4|hex6)$"
61-
regex2 = r"^\-(replace|append)$"
6265
if (re.search(regex1, param1) != None):
6366
conversion_mode = re.findall(regex1, param1)[0]
6467
elif (re.search(regex2, param1) != None):
@@ -74,4 +77,5 @@ def main():
7477

7578
else :
7679
print ("You must enter at least 2 parameters.")
80+
print ("Run \"ip2location-csv-converter -help\" to learn on the available parameters.")
7781
sys.exit(1)

ip2location_csv_converter/ip2location_csv_converter.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
# chunk_size = 10000
1010

1111
def no2ip(iplong):
12-
if (int(iplong) > 4294967295):
13-
if sys.version < '3':
14-
return ipaddress.ip_address(long(iplong)).__str__()
12+
try:
13+
if (int(iplong) > 4294967295):
14+
if sys.version < '3':
15+
return ipaddress.ip_address(long(iplong)).__str__()
16+
else:
17+
return ipaddress.ip_address(int(iplong)).__str__()
1518
else:
16-
return ipaddress.ip_address(int(iplong)).__str__()
17-
else:
18-
return (socket.inet_ntoa(struct.pack('!I', int(iplong))))
19+
return (socket.inet_ntoa(struct.pack('!I', int(iplong))))
20+
except ValueError:
21+
print(f'Invalid IP number value {iplong} detected. Please fix it before continue.')
22+
sys.exit(1)
1923

2024
def check_data_validity(file):
2125
with open(file, newline = "") as csvfile:
@@ -28,7 +32,7 @@ def check_data_validity(file):
2832
def range_number_to_ip(row, write_mode):
2933
from_ip = no2ip(row[0])
3034
to_ip = no2ip(row[1])
31-
# print (from_ip, to_ip)
35+
print (from_ip, to_ip)
3236
total_row = len(row)
3337
remaining_columns = ''
3438
for i in range(2, total_row):
@@ -163,7 +167,7 @@ def convert_to_csv(input_file, output_file, conversion_mode, write_mode):
163167
my_list.append(new_row.decode('utf-8'))
164168
else:
165169
# detect_eol(new_row)
166-
print(repr(new_row))
170+
# print(repr(new_row))
167171
my_list.append(new_row)
168172

169173
if (len(my_list) > 0):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setuptools.setup(
1212
name="ip2location-python-csv-converter",
13-
version="1.2.6",
13+
version="1.2.7",
1414
description="Python script to converts IP2Location CSV database into IP range or CIDR format.",
1515
long_description_content_type="text/markdown",
1616
long_description=long_description,

0 commit comments

Comments
 (0)