Posts: 4,647
Threads: 1,494
Joined: Sep 2016
i am looking for a command that can sort a file (or stream from stdin to stdout) much like the posix/unix/linux sort command, but based on the key being an IP address. bonuses: can you do it with just modules that come with Python? can you include support for IPv6?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 544
Threads: 15
Joined: Oct 2016
A list to test would make it easier.
Here something I threw together. Might get you started.
ip_address = [
'192.168.0.1',
'192.168.0.5',
'20.101.24.5',
'101.17.31.8',
'41.37.101.40',
'41.37.101.4'
]
def sort_ip(key):
if key.count('.') == 3:
return 4, [k.rjust(3) for k in key.split('.')]
else:
# need more data for dealing with ipv6 address
return 6, [k.rjust(4) for k in key.split(':')]
def main():
ip_address.sort(key=sort_ip)
for ip in ip_address:
print(ip)
main()
99 percent of computer problems exists between chair and keyboard.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Oct-22-2017, 01:41 AM
(This post was last modified: Oct-22-2017, 01:41 AM by Skaperen.)
needs to be able to sort a
file. what if the file is massively huge (larger than available virtual memory)?
how does your code compare
fc00::1 with
fc00:0:2::1 ?
how does your code deal with addresses like:
fc00::1.2.3.4 ?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
what about all the ips in fc00:/7 (fc00:: to fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff)?
what about any ipv6 (in ::/0)
is module ipaddress in Python2.6? 3.4? 3.3?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.