title: Conversion from IP string to integer, and backward in Python
body: <p>i have a little problem with my script, where i need to convert ip in form 'xxx.xxx.xxx.xxx' to integer representation and go back from this form.</p>
  
  <pre><code>def iptoint(ip):
      return int(socket.inet_aton(ip).encode('hex'),16)
  
  def inttoip(ip):
      return socket.inet_ntoa(hex(ip)[2:].decode('hex'))
  
  
  In [65]: inttoip(iptoint('192.168.1.1'))
  Out[65]: '192.168.1.1'
  
  In [66]: inttoip(iptoint('4.1.75.131'))
  ---------------------------------------------------------------------------
  error                                     Traceback (most recent call last)
  
  /home/thc/&lt;ipython console&gt; in &lt;module&gt;()
  
  /home/thc/&lt;ipython console&gt; in inttoip(ip)
  
  error: packed IP wrong length for inet_ntoa`
  </code></pre>
  
  <p>Anybody knows how to fix that?</p>