<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web-In-Sight &#187; address</title>
	<atom:link href="http://web-in-sight.nl/tag/address/feed/" rel="self" type="application/rss+xml" />
	<link>http://web-in-sight.nl</link>
	<description>Inzicht in internet en werken</description>
	<lastBuildDate>Mon, 30 Jan 2012 09:00:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A python function to capture an IP address or range</title>
		<link>http://web-in-sight.nl/2008/06/12/a-python-function-to-capture-an-ip-address-or-range/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-python-function-to-capture-an-ip-address-or-range</link>
		<comments>http://web-in-sight.nl/2008/06/12/a-python-function-to-capture-an-ip-address-or-range/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 10:39:02 +0000</pubDate>
		<dc:creator>Gerard</dc:creator>
				<category><![CDATA[All ENGLISH articles]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[netmask]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.gp-net.nl/?p=49</guid>
		<description><![CDATA[I recently needed a function that validates an IP address or network range. Since my python application will pass it as a parameter to iptables it needs to be correct and not &#8216;close to&#8217;. So I dug in &#8230; Validating &#8230; <a href="http://web-in-sight.nl/2008/06/12/a-python-function-to-capture-an-ip-address-or-range/">Lees verder <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently needed a function that validates an IP address or network range. Since my python application will pass it as a parameter to iptables it needs to be correct and not &#8216;close to&#8217;. So I dug in &#8230;</p>
<p>Validating an IP address or range with just a regex seems like self castigation. I looked at the source code of iptables and it check&#8217;s whether or not 1 octet fits in a byte. With your octet being only valid from 0 up to and 255 it must fit in 1 byte. That method seems ok but when you&#8217;re writing interpretable code the interpreter most likely does a better job then you in checking byte lenght&#8217;s. If it doesn&#8217;t already do it like that when checking int&#8217;s like this:</p>
<pre>if not (0 &lt;= int(octet) &lt;= 255):</pre>
<p>Digesting all that information I wrote the function below that takes an IP address or range and simply returns &#8216;True&#8217; or &#8216;False&#8217;.</p>
<pre>def check_address(address):

    if not (re.search('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(|\/\d{1,2})$', address)):
        return False

    if (address.count('/') == 1):
        (ip, mask) = address.split('/')
        if not (0 &lt;= int(mask) &lt;= 32):
            return False
    else:
        ip = address

    for octet in ip.split('.'):
        if not (0 &lt;= int(octet) &lt;= 255):
            return False

    return True</pre>
<p>IMHO, it&#8217;s very safe and very readable. You know &#8230; <a title="KISS" href="http://en.wikipedia.org/wiki/KISS_principle" target="_blank">KISS</a>.</p>
<p>If you have suggestions on how to do this more pythonesque I&#8217;m very curious to here them so please drop me a line.</p>
<p>GrtzG</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fweb-in-sight.nl%2F2008%2F06%2F12%2Fa-python-function-to-capture-an-ip-address-or-range%2F&amp;send=false&amp;layout=button_count&amp;width=&amp;show_faces=false&amp;action=recommend&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:21px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://web-in-sight.nl/2008/06/12/a-python-function-to-capture-an-ip-address-or-range/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

