Last active
June 1, 2021 18:47
-
-
Save wesinator/c18b53ad32b2eb353dfc52139c55d869 to your computer and use it in GitHub Desktop.
See https://netaddr.readthedocs.io/en/latest/tutorial_01.html#public for a faster version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import time\n", | |
"\n", | |
"# https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution\n", | |
"def timeit(method):\n", | |
" def timed(*args, **kw):\n", | |
" ts = time.time()\n", | |
" result = method(*args, **kw)\n", | |
" te = time.time()\n", | |
" if 'log_time' in kw:\n", | |
" name = kw.get('log_name', method.__name__.upper())\n", | |
" kw['log_time'][name] = int((te - ts) * 1000)\n", | |
" else:\n", | |
" print('%r %2.22f ms' % (method.__name__, (te - ts) * 1000))\n", | |
" return result\n", | |
" return timed\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"'test_ipy' 12.1805667877197265625000 ms\n", | |
"'test_ipy' 12.0871067047119140625000 ms\n", | |
"'test_ipy' 13.1344795227050781250000 ms\n", | |
"'test_ipy' 13.1304264068603515625000 ms\n", | |
"'test_ipy' 17.9247856140136718750000 ms\n", | |
"'test_ipy' 15.7623291015625000000000 ms\n", | |
"'test_ipy' 13.6168003082275390625000 ms\n", | |
"'test_ipy' 14.3928527832031250000000 ms\n", | |
"'test_ipy' 12.9981040954589843750000 ms\n", | |
"'test_ipy' 9.5784664154052734375000 ms\n", | |
"'test_ipy' 6.8905353546142578125000 ms\n", | |
"'test_ipy' 5.7108402252197265625000 ms\n", | |
"'test_ipy' 5.9533119201660156250000 ms\n", | |
"'test_ipy' 4.7328472137451171875000 ms\n", | |
"'test_ipy' 4.6195983886718750000000 ms\n", | |
"'test_ipy' 4.6648979187011718750000 ms\n", | |
"'test_ipy' 4.6958923339843750000000 ms\n", | |
"'test_ipy' 4.7779083251953125000000 ms\n", | |
"'test_ipy' 4.6169757843017578125000 ms\n", | |
"'test_ipy' 4.6083927154541015625000 ms\n", | |
"'test_ipy' 4.6417713165283203125000 ms\n", | |
"'test_ipy' 4.5855045318603515625000 ms\n", | |
"'test_ipy' 4.5812129974365234375000 ms\n", | |
"'test_ipy' 4.6098232269287109375000 ms\n", | |
"'test_ipy' 4.7094821929931640625000 ms\n", | |
"'test_ipy' 4.6010017395019531250000 ms\n", | |
"'test_ipy' 4.6498775482177734375000 ms\n", | |
"'test_ipy' 4.8079490661621093750000 ms\n", | |
"'test_ipy' 4.6503543853759765625000 ms\n", | |
"'test_ipy' 4.6358108520507812500000 ms\n", | |
"'test_ipy' 4.4434070587158203125000 ms\n", | |
"'test_ipy' 3.9348602294921875000000 ms\n", | |
"'test_ipy' 3.8113594055175781250000 ms\n", | |
"'test_ipy' 3.4761428833007812500000 ms\n", | |
"'test_ipy' 3.7164688110351562500000 ms\n", | |
"'test_ipy' 3.2424926757812500000000 ms\n", | |
"'test_ipy' 4.3959617614746093750000 ms\n", | |
"'test_ipy' 3.2651424407958984375000 ms\n", | |
"'test_ipy' 2.5925636291503906250000 ms\n", | |
"'test_ipy' 2.8045177459716796875000 ms\n", | |
"'test_ipy' 3.2315254211425781250000 ms\n", | |
"'test_ipy' 3.4928321838378906250000 ms\n", | |
"'test_ipy' 2.4447441101074218750000 ms\n", | |
"'test_ipy' 2.3074150085449218750000 ms\n", | |
"'test_ipy' 2.3038387298583984375000 ms\n", | |
"'test_ipy' 2.3014545440673828125000 ms\n", | |
"'test_ipy' 2.2945404052734375000000 ms\n", | |
"'test_ipy' 2.9177665710449218750000 ms\n", | |
"'test_ipy' 2.3963451385498046875000 ms\n", | |
"'test_ipy' 2.4187564849853515625000 ms\n", | |
"'test_ipy' 2.3763179779052734375000 ms\n", | |
"'test_ipy' 2.3086071014404296875000 ms\n", | |
"'test_ipy' 2.3095607757568359375000 ms\n", | |
"'test_ipy' 2.3760795593261718750000 ms\n", | |
"'test_ipy' 2.5000572204589843750000 ms\n", | |
"'test_ipy' 2.4411678314208984375000 ms\n", | |
"'test_ipy' 2.3880004882812500000000 ms\n", | |
"'test_ipy' 2.3088455200195312500000 ms\n", | |
"'test_ipy' 2.3002624511718750000000 ms\n", | |
"'test_ipy' 2.3419857025146484375000 ms\n", | |
"'test_ipy' 2.3074150085449218750000 ms\n", | |
"'test_ipy' 2.3736953735351562500000 ms\n", | |
"'test_ipy' 2.3300647735595703125000 ms\n", | |
"'test_ipy' 2.3157596588134765625000 ms\n", | |
"'test_ipy' 2.3248195648193359375000 ms\n", | |
"'test_ipy' 2.9587745666503906250000 ms\n", | |
"'test_ipy' 2.9501914978027343750000 ms\n", | |
"'test_ipy' 6.8175792694091796875000 ms\n", | |
"'test_ipy' 3.3960342407226562500000 ms\n", | |
"'test_ipy' 2.3586750030517578125000 ms\n", | |
"'test_ipy' 2.3300647735595703125000 ms\n", | |
"'test_ipy' 2.4828910827636718750000 ms\n", | |
"'test_ipy' 2.6307106018066406250000 ms\n", | |
"'test_ipy' 3.0343532562255859375000 ms\n", | |
"'test_ipy' 2.7284622192382812500000 ms\n", | |
"'test_ipy' 2.3384094238281250000000 ms\n", | |
"'test_ipy' 2.3317337036132812500000 ms\n", | |
"'test_ipy' 2.8114318847656250000000 ms\n", | |
"'test_ipy' 3.0250549316406250000000 ms\n", | |
"'test_ipy' 2.4540424346923828125000 ms\n", | |
"'test_ipy' 2.3150444030761718750000 ms\n", | |
"'test_ipy' 2.2959709167480468750000 ms\n", | |
"'test_ipy' 2.3074150085449218750000 ms\n", | |
"'test_ipy' 2.3949146270751953125000 ms\n", | |
"'test_ipy' 2.5267601013183593750000 ms\n", | |
"'test_ipy' 3.0457973480224609375000 ms\n", | |
"'test_ipy' 2.4044513702392578125000 ms\n", | |
"'test_ipy' 2.3250579833984375000000 ms\n", | |
"'test_ipy' 2.3233890533447265625000 ms\n", | |
"'test_ipy' 2.3317337036132812500000 ms\n", | |
"'test_ipy' 2.3105144500732421875000 ms\n", | |
"'test_ipy' 2.2752285003662109375000 ms\n", | |
"'test_ipy' 2.2845268249511718750000 ms\n", | |
"'test_ipy' 2.3047924041748046875000 ms\n", | |
"'test_ipy' 2.2957324981689453125000 ms\n", | |
"'test_ipy' 2.2683143615722656250000 ms\n", | |
"'test_ipy' 2.3155212402343750000000 ms\n", | |
"'test_ipy' 2.3326873779296875000000 ms\n", | |
"'test_ipy' 2.2902488708496093750000 ms\n", | |
"'test_ipy' 2.2990703582763671875000 ms\n", | |
"'test_ipy' 2.2904872894287109375000 ms\n", | |
"'test_ipy' 2.3069381713867187500000 ms\n", | |
"'test_ipy' 2.3257732391357421875000 ms\n", | |
"'test_ipy' 2.3760795593261718750000 ms\n", | |
"'test_ipy' 2.3064613342285156250000 ms\n", | |
"'test_ipy' 2.3076534271240234375000 ms\n", | |
"'test_ipy' 2.2954940795898437500000 ms\n", | |
"'test_ipy' 2.3009777069091796875000 ms\n", | |
"'test_ipy' 2.2940635681152343750000 ms\n", | |
"'test_ipy' 2.3105144500732421875000 ms\n", | |
"'test_ipy' 2.3093223571777343750000 ms\n", | |
"'test_ipy' 2.3527145385742187500000 ms\n", | |
"'test_ipy' 2.3238658905029296875000 ms\n", | |
"'test_ipy' 2.3169517517089843750000 ms\n", | |
"'test_ipy' 2.3124217987060546875000 ms\n", | |
"'test_ipy' 2.3174285888671875000000 ms\n", | |
"'test_ipy' 2.3219585418701171875000 ms\n", | |
"'test_ipy' 2.3045539855957031250000 ms\n", | |
"'test_ipy' 2.2993087768554687500000 ms\n", | |
"'test_ipy' 2.3162364959716796875000 ms\n", | |
"'test_ipy' 2.2966861724853515625000 ms\n", | |
"'test_ipy' 2.3176670074462890625000 ms\n", | |
"'test_ipy' 2.3300647735595703125000 ms\n", | |
"'test_ipy' 2.3033618927001953125000 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from IPy import IP\n", | |
"\n", | |
"ips = ['198.5.0.' + str(i) for i in range(0, 256)]\n", | |
"\n", | |
"@timeit\n", | |
"def test_ipy(ips):\n", | |
" for ip in ips:\n", | |
" iptype = IP(ip).iptype()\n", | |
" if iptype == \"PRIVATE\":\n", | |
" break\n", | |
"\n", | |
"times = []\n", | |
"for i in range(0, 124):\n", | |
" test_ipy(ips)\n", | |
"times\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"'test_netaddr' 6.6406726837158203125000 ms\n", | |
"'test_netaddr' 9.8690986633300781250000 ms\n", | |
"'test_netaddr' 5.9850215911865234375000 ms\n", | |
"'test_netaddr' 5.8040618896484375000000 ms\n", | |
"'test_netaddr' 8.1243515014648437500000 ms\n", | |
"'test_netaddr' 8.8083744049072265625000 ms\n", | |
"'test_netaddr' 6.0613155364990234375000 ms\n", | |
"'test_netaddr' 10.2503299713134765625000 ms\n", | |
"'test_netaddr' 9.3774795532226562500000 ms\n", | |
"'test_netaddr' 7.2643756866455078125000 ms\n", | |
"'test_netaddr' 7.1945190429687500000000 ms\n", | |
"'test_netaddr' 6.8998336791992187500000 ms\n", | |
"'test_netaddr' 10.6165409088134765625000 ms\n", | |
"'test_netaddr' 6.3240528106689453125000 ms\n", | |
"'test_netaddr' 4.5952796936035156250000 ms\n", | |
"'test_netaddr' 3.7322044372558593750000 ms\n", | |
"'test_netaddr' 3.9341449737548828125000 ms\n", | |
"'test_netaddr' 3.3335685729980468750000 ms\n", | |
"'test_netaddr' 2.8069019317626953125000 ms\n", | |
"'test_netaddr' 2.6123523712158203125000 ms\n", | |
"'test_netaddr' 2.9520988464355468750000 ms\n", | |
"'test_netaddr' 2.4108886718750000000000 ms\n", | |
"'test_netaddr' 2.4070739746093750000000 ms\n", | |
"'test_netaddr' 1.8925666809082031250000 ms\n", | |
"'test_netaddr' 1.8823146820068359375000 ms\n", | |
"'test_netaddr' 1.8742084503173828125000 ms\n", | |
"'test_netaddr' 1.8777847290039062500000 ms\n", | |
"'test_netaddr' 1.7812252044677734375000 ms\n", | |
"'test_netaddr' 1.7673969268798828125000 ms\n", | |
"'test_netaddr' 1.7356872558593750000000 ms\n", | |
"'test_netaddr' 1.6300678253173828125000 ms\n", | |
"'test_netaddr' 1.6329288482666015625000 ms\n", | |
"'test_netaddr' 1.5542507171630859375000 ms\n", | |
"'test_netaddr' 1.4386177062988281250000 ms\n", | |
"'test_netaddr' 1.4085769653320312500000 ms\n", | |
"'test_netaddr' 1.4107227325439453125000 ms\n", | |
"'test_netaddr' 2.1014213562011718750000 ms\n", | |
"'test_netaddr' 1.4441013336181640625000 ms\n", | |
"'test_netaddr' 1.2874603271484375000000 ms\n", | |
"'test_netaddr' 1.2626647949218750000000 ms\n", | |
"'test_netaddr' 1.2459754943847656250000 ms\n", | |
"'test_netaddr' 1.2447834014892578125000 ms\n", | |
"'test_netaddr' 1.2567043304443359375000 ms\n", | |
"'test_netaddr' 1.2454986572265625000000 ms\n", | |
"'test_netaddr' 1.2965202331542968750000 ms\n", | |
"'test_netaddr' 1.1761188507080078125000 ms\n", | |
"'test_netaddr' 1.1553764343261718750000 ms\n", | |
"'test_netaddr' 1.1286735534667968750000 ms\n", | |
"'test_netaddr' 1.3570785522460937500000 ms\n", | |
"'test_netaddr' 1.1317729949951171875000 ms\n", | |
"'test_netaddr' 1.1246204376220703125000 ms\n", | |
"'test_netaddr' 1.1591911315917968750000 ms\n", | |
"'test_netaddr' 1.1329650878906250000000 ms\n", | |
"'test_netaddr' 1.1296272277832031250000 ms\n", | |
"'test_netaddr' 1.1706352233886718750000 ms\n", | |
"'test_netaddr' 1.1296272277832031250000 ms\n", | |
"'test_netaddr' 1.1222362518310546875000 ms\n", | |
"'test_netaddr' 1.1222362518310546875000 ms\n", | |
"'test_netaddr' 1.1420249938964843750000 ms\n", | |
"'test_netaddr' 1.1246204376220703125000 ms\n", | |
"'test_netaddr' 1.1274814605712890625000 ms\n", | |
"'test_netaddr' 1.1258125305175781250000 ms\n", | |
"'test_netaddr' 1.1274814605712890625000 ms\n", | |
"'test_netaddr' 1.1246204376220703125000 ms\n", | |
"'test_netaddr' 1.1212825775146484375000 ms\n", | |
"'test_netaddr' 1.1591911315917968750000 ms\n", | |
"'test_netaddr' 1.1246204376220703125000 ms\n", | |
"'test_netaddr' 1.7714500427246093750000 ms\n", | |
"'test_netaddr' 1.1348724365234375000000 ms\n", | |
"'test_netaddr' 1.1222362518310546875000 ms\n", | |
"'test_netaddr' 1.1353492736816406250000 ms\n", | |
"'test_netaddr' 1.1274814605712890625000 ms\n", | |
"'test_netaddr' 1.1284351348876953125000 ms\n", | |
"'test_netaddr' 1.1265277862548828125000 ms\n", | |
"'test_netaddr' 1.1236667633056640625000 ms\n", | |
"'test_netaddr' 1.1324882507324218750000 ms\n", | |
"'test_netaddr' 1.1253356933593750000000 ms\n", | |
"'test_netaddr' 1.1329650878906250000000 ms\n", | |
"'test_netaddr' 1.2004375457763671875000 ms\n", | |
"'test_netaddr' 2.0234584808349609375000 ms\n", | |
"'test_netaddr' 1.2001991271972656250000 ms\n", | |
"'test_netaddr' 1.5428066253662109375000 ms\n", | |
"'test_netaddr' 1.1448860168457031250000 ms\n", | |
"'test_netaddr' 1.1694431304931640625000 ms\n", | |
"'test_netaddr' 1.1479854583740234375000 ms\n", | |
"'test_netaddr' 1.1401176452636718750000 ms\n", | |
"'test_netaddr' 1.1250972747802734375000 ms\n", | |
"'test_netaddr' 1.1637210845947265625000 ms\n", | |
"'test_netaddr' 1.1534690856933593750000 ms\n", | |
"'test_netaddr' 1.1925697326660156250000 ms\n", | |
"'test_netaddr' 1.9116401672363281250000 ms\n", | |
"'test_netaddr' 1.2140274047851562500000 ms\n", | |
"'test_netaddr' 1.1975765228271484375000 ms\n", | |
"'test_netaddr' 1.4467239379882812500000 ms\n", | |
"'test_netaddr' 1.1522769927978515625000 ms\n", | |
"'test_netaddr' 1.1253356933593750000000 ms\n", | |
"'test_netaddr' 1.1272430419921875000000 ms\n", | |
"'test_netaddr' 1.1305809020996093750000 ms\n", | |
"'test_netaddr' 1.1265277862548828125000 ms\n", | |
"'test_netaddr' 1.1253356933593750000000 ms\n", | |
"'test_netaddr' 1.1258125305175781250000 ms\n", | |
"'test_netaddr' 1.1293888092041015625000 ms\n", | |
"'test_netaddr' 1.1672973632812500000000 ms\n", | |
"'test_netaddr' 1.2176036834716796875000 ms\n", | |
"'test_netaddr' 1.1384487152099609375000 ms\n", | |
"'test_netaddr' 1.1246204376220703125000 ms\n", | |
"'test_netaddr' 1.1224746704101562500000 ms\n", | |
"'test_netaddr' 1.1289119720458984375000 ms\n", | |
"'test_netaddr' 1.1236667633056640625000 ms\n", | |
"'test_netaddr' 1.1260509490966796875000 ms\n", | |
"'test_netaddr' 1.1281967163085937500000 ms\n", | |
"'test_netaddr' 1.1248588562011718750000 ms\n", | |
"'test_netaddr' 1.1229515075683593750000 ms\n", | |
"'test_netaddr' 1.1241436004638671875000 ms\n", | |
"'test_netaddr' 1.1296272277832031250000 ms\n", | |
"'test_netaddr' 1.1477470397949218750000 ms\n", | |
"'test_netaddr' 1.1897087097167968750000 ms\n", | |
"'test_netaddr' 1.2466907501220703125000 ms\n", | |
"'test_netaddr' 1.1267662048339843750000 ms\n", | |
"'test_netaddr' 1.1332035064697265625000 ms\n", | |
"'test_netaddr' 1.2774467468261718750000 ms\n", | |
"'test_netaddr' 1.1408329010009765625000 ms\n", | |
"'test_netaddr' 1.1231899261474609375000 ms\n", | |
"'test_netaddr' 1.1191368103027343750000 ms\n", | |
"'test_netaddr' 1.1234283447265625000000 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from netaddr import IPAddress\n", | |
"\n", | |
"ips = ['198.5.0.' + str(i) for i in range(0, 256)]\n", | |
"\n", | |
"@timeit\n", | |
"def test_netaddr(ips):\n", | |
" for ip in ips:\n", | |
" if IPAddress(ip).is_private():\n", | |
" break\n", | |
"test_netaddr(ips)\n", | |
"\n", | |
"\n", | |
"times = []\n", | |
"for i in range(0, 124):\n", | |
" test_netaddr(ips)\n", | |
"times\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"'test_ipaddress' 9.4726085662841796875000 ms\n", | |
"'test_ipaddress' 9.0229511260986328125000 ms\n", | |
"'test_ipaddress' 8.9526176452636718750000 ms\n", | |
"'test_ipaddress' 9.2470645904541015625000 ms\n", | |
"'test_ipaddress' 11.7692947387695312500000 ms\n", | |
"'test_ipaddress' 10.5938911437988281250000 ms\n", | |
"'test_ipaddress' 10.1397037506103515625000 ms\n", | |
"'test_ipaddress' 9.6602439880371093750000 ms\n", | |
"'test_ipaddress' 13.7271881103515625000000 ms\n", | |
"'test_ipaddress' 11.7366313934326171875000 ms\n", | |
"'test_ipaddress' 11.4772319793701171875000 ms\n", | |
"'test_ipaddress' 9.6282958984375000000000 ms\n", | |
"'test_ipaddress' 9.4361305236816406250000 ms\n", | |
"'test_ipaddress' 9.1664791107177734375000 ms\n", | |
"'test_ipaddress' 7.3516368865966796875000 ms\n", | |
"'test_ipaddress' 7.0385932922363281250000 ms\n", | |
"'test_ipaddress' 4.3289661407470703125000 ms\n", | |
"'test_ipaddress' 4.0063858032226562500000 ms\n", | |
"'test_ipaddress' 3.3752918243408203125000 ms\n", | |
"'test_ipaddress' 3.3755302429199218750000 ms\n", | |
"'test_ipaddress' 3.0658245086669921875000 ms\n", | |
"'test_ipaddress' 2.8321743011474609375000 ms\n", | |
"'test_ipaddress' 2.8631687164306640625000 ms\n", | |
"'test_ipaddress' 2.7740001678466796875000 ms\n", | |
"'test_ipaddress' 2.4085044860839843750000 ms\n", | |
"'test_ipaddress' 2.4001598358154296875000 ms\n", | |
"'test_ipaddress' 2.4402141571044921875000 ms\n", | |
"'test_ipaddress' 2.1467208862304687500000 ms\n", | |
"'test_ipaddress' 2.0775794982910156250000 ms\n", | |
"'test_ipaddress' 2.0821094512939453125000 ms\n", | |
"'test_ipaddress' 2.0809173583984375000000 ms\n", | |
"'test_ipaddress' 1.9457340240478515625000 ms\n", | |
"'test_ipaddress' 1.8570423126220703125000 ms\n", | |
"'test_ipaddress' 1.8489360809326171875000 ms\n", | |
"'test_ipaddress' 1.8432140350341796875000 ms\n", | |
"'test_ipaddress' 1.8784999847412109375000 ms\n", | |
"'test_ipaddress' 1.7156600952148437500000 ms\n", | |
"'test_ipaddress' 1.6717910766601562500000 ms\n", | |
"'test_ipaddress' 1.6763210296630859375000 ms\n", | |
"'test_ipaddress' 1.6624927520751953125000 ms\n", | |
"'test_ipaddress' 1.6803741455078125000000 ms\n", | |
"'test_ipaddress' 1.6722679138183593750000 ms\n", | |
"'test_ipaddress' 1.8308162689208984375000 ms\n", | |
"'test_ipaddress' 1.6975402832031250000000 ms\n", | |
"'test_ipaddress' 1.7013549804687500000000 ms\n", | |
"'test_ipaddress' 1.9001960754394531250000 ms\n", | |
"'test_ipaddress' 2.6547908782958984375000 ms\n", | |
"'test_ipaddress' 2.7379989624023437500000 ms\n", | |
"'test_ipaddress' 3.7984848022460937500000 ms\n", | |
"'test_ipaddress' 3.5109519958496093750000 ms\n", | |
"'test_ipaddress' 2.9830932617187500000000 ms\n", | |
"'test_ipaddress' 2.7508735656738281250000 ms\n", | |
"'test_ipaddress' 1.7759799957275390625000 ms\n", | |
"'test_ipaddress' 1.6753673553466796875000 ms\n", | |
"'test_ipaddress' 1.6720294952392578125000 ms\n", | |
"'test_ipaddress' 1.6837120056152343750000 ms\n", | |
"'test_ipaddress' 1.6763210296630859375000 ms\n", | |
"'test_ipaddress' 1.6899108886718750000000 ms\n", | |
"'test_ipaddress' 1.8091201782226562500000 ms\n", | |
"'test_ipaddress' 1.7762184143066406250000 ms\n", | |
"'test_ipaddress' 1.7104148864746093750000 ms\n", | |
"'test_ipaddress' 1.8882751464843750000000 ms\n", | |
"'test_ipaddress' 1.6977787017822265625000 ms\n", | |
"'test_ipaddress' 1.6736984252929687500000 ms\n", | |
"'test_ipaddress' 1.6701221466064453125000 ms\n", | |
"'test_ipaddress' 1.6670227050781250000000 ms\n", | |
"'test_ipaddress' 1.6620159149169921875000 ms\n", | |
"'test_ipaddress' 2.1905899047851562500000 ms\n", | |
"'test_ipaddress' 1.8334388732910156250000 ms\n", | |
"'test_ipaddress' 1.8839836120605468750000 ms\n", | |
"'test_ipaddress' 1.8656253814697265625000 ms\n", | |
"'test_ipaddress' 1.9681453704833984375000 ms\n", | |
"'test_ipaddress' 1.7609596252441406250000 ms\n", | |
"'test_ipaddress' 1.6894340515136718750000 ms\n", | |
"'test_ipaddress' 1.6784667968750000000000 ms\n", | |
"'test_ipaddress' 1.6710758209228515625000 ms\n", | |
"'test_ipaddress' 2.0129680633544921875000 ms\n", | |
"'test_ipaddress' 1.8250942230224609375000 ms\n", | |
"'test_ipaddress' 1.8253326416015625000000 ms\n", | |
"'test_ipaddress' 1.6961097717285156250000 ms\n", | |
"'test_ipaddress' 1.6601085662841796875000 ms\n", | |
"'test_ipaddress' 1.6689300537109375000000 ms\n", | |
"'test_ipaddress' 1.6574859619140625000000 ms\n", | |
"'test_ipaddress' 1.6679763793945312500000 ms\n", | |
"'test_ipaddress' 1.6601085662841796875000 ms\n", | |
"'test_ipaddress' 1.6961097717285156250000 ms\n", | |
"'test_ipaddress' 1.7721652984619140625000 ms\n", | |
"'test_ipaddress' 1.6622543334960937500000 ms\n", | |
"'test_ipaddress' 1.6727447509765625000000 ms\n", | |
"'test_ipaddress' 1.6634464263916015625000 ms\n", | |
"'test_ipaddress' 1.6660690307617187500000 ms\n", | |
"'test_ipaddress' 1.7731189727783203125000 ms\n", | |
"'test_ipaddress' 1.6694068908691406250000 ms\n", | |
"'test_ipaddress' 1.6720294952392578125000 ms\n", | |
"'test_ipaddress' 1.6610622406005859375000 ms\n", | |
"'test_ipaddress' 1.7256736755371093750000 ms\n", | |
"'test_ipaddress' 1.7161369323730468750000 ms\n", | |
"'test_ipaddress' 1.6703605651855468750000 ms\n", | |
"'test_ipaddress' 1.6725063323974609375000 ms\n", | |
"'test_ipaddress' 1.6655921936035156250000 ms\n", | |
"'test_ipaddress' 1.7735958099365234375000 ms\n", | |
"'test_ipaddress' 1.6672611236572265625000 ms\n", | |
"'test_ipaddress' 1.6701221466064453125000 ms\n", | |
"'test_ipaddress' 1.6682147979736328125000 ms\n", | |
"'test_ipaddress' 1.6865730285644531250000 ms\n", | |
"'test_ipaddress' 2.0246505737304687500000 ms\n", | |
"'test_ipaddress' 1.6798973083496093750000 ms\n", | |
"'test_ipaddress' 1.6894340515136718750000 ms\n", | |
"'test_ipaddress' 1.6872882843017578125000 ms\n", | |
"'test_ipaddress' 1.7938613891601562500000 ms\n", | |
"'test_ipaddress' 1.6925334930419921875000 ms\n", | |
"'test_ipaddress' 1.6756057739257812500000 ms\n", | |
"'test_ipaddress' 1.6677379608154296875000 ms\n", | |
"'test_ipaddress' 1.6613006591796875000000 ms\n", | |
"'test_ipaddress' 2.0687580108642578125000 ms\n", | |
"'test_ipaddress' 1.7609596252441406250000 ms\n", | |
"'test_ipaddress' 1.6896724700927734375000 ms\n", | |
"'test_ipaddress' 1.6748905181884765625000 ms\n", | |
"'test_ipaddress' 1.7735958099365234375000 ms\n", | |
"'test_ipaddress' 1.7118453979492187500000 ms\n", | |
"'test_ipaddress' 1.6696453094482421875000 ms\n", | |
"'test_ipaddress' 1.6789436340332031250000 ms\n", | |
"'test_ipaddress' 1.6696453094482421875000 ms\n", | |
"'test_ipaddress' 1.9056797027587890625000 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[]" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ipaddress\n", | |
"\n", | |
"ips = ['198.5.0.' + str(i) for i in range(0, 256)]\n", | |
"\n", | |
"@timeit\n", | |
"def test_ipaddress(ips):\n", | |
" for ip in ips:\n", | |
" ip = ipaddress.ip_address(ip)\n", | |
" if ip.is_multicast or ip.is_private:\n", | |
" print(ip)\n", | |
" break\n", | |
"\n", | |
"times = []\n", | |
"for i in range(0, 124):\n", | |
" test_ipaddress(ips)\n", | |
"times\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ipaddress\n", | |
"\n", | |
"ip = \"10.5.4.6\"\n", | |
"ip = ipaddress.ip_address(ip)\n", | |
"ip.is_private\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True True False\n" | |
] | |
} | |
], | |
"source": [ | |
"import ipaddress\n", | |
"\n", | |
"def checkip(ip):\n", | |
" ip = ipaddress.ip_address(ip)\n", | |
" return ip.is_private or ip.is_multicast\n", | |
"\n", | |
"print(checkip(\"224.0.0.1\"),\n", | |
"checkip(\"10.0.2.2\"),\n", | |
"checkip(\"1.1.1.1\"),\n", | |
" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from IPy import IP | |
def getIPtype(ip): | |
try: | |
return IP(ip).iptype() | |
except ValueError: | |
return None | |
def ipPublic(ip): | |
if getIPtype(ip) == "PUBLIC": | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment