Python: Performing Mathematical Functions between IP Address Octets
<p>I'm trying to perform the following:</p>

<pre><code>firstoctet * 256 + secondoctet = * 256 + thirdoctet = * 256 + fourthoctet = x
</code></pre>

<p>When I use this as an example :</p>

<pre><code>64.233.187.99 (google.com)
64 * 256 + 233 = * 256 + 187 = * 256 + 99 = http://1089059683/
</code></pre>

<p>Can someone please provide a method as to how this can be done? The mathematical sequence is no problem, i'm just unsure as to how I can take the octet values out of the decimal points to perform the math functions. </p>

<p>Thanks in advance!</p>

<p>Convert an IP address string to a list of integers like this:</p>

<pre><code>ip_as_string = "64.233.187.99"
ip_as_ints = [int(a) for a in ip_as_string.split('.')]
</code></pre>

<p><code>ip_as_ints</code> will be [64, 233, 187, 99].</p>

<p>I will guess that the mathematical expression that you intend to perform is RPN for converting the byte sequence from a base-256 representation to a single number.  (Thanks to ShadowRanger for showing this).  You can obtain that single number easily as follows:</p>

<pre><code>x = functools.reduce(lambda x,y: (x &lt;&lt; 8) | y, ip_as_ints, 0)
print(x)
</code></pre>

<p>x is 1089059683.</p>

python python-2.7 python-3.x