title: Error using GetExtendedTcpTable in python
body: <p>I've got some troubles with using "GetExtendedTcpTable". When I tried to run my script, i've got message like this:</p>
  
  <blockquote>
    <p>AssertionError: [Error 0] The operation completed successfully</p>
  </blockquote>
  
  <p>Rarely script working normally, I dont understand this message, Operation completed, what`s wrong?</p>
  
  <p>This is code, i tried to execute:</p>
  
  <pre><code>from ctypes import *
  from ctypes.wintypes import *
  from socket import  inet_aton,   inet_ntoa,  htons
  
  
  AF_INET = 2
  TCP_TABLE_BASIC_LISTENER = 0
  TCP_TABLE_BASIC_CONNECTIONS = 1
  TCP_TABLE_BASIC_ALL = 2
  TCP_TABLE_OWNER_PID_LISTENER = 3
  TCP_TABLE_OWNER_PID_CONNECTIONS = 4
  TCP_TABLE_OWNER_PID_ALL = 5
  TCP_TABLE_OWNER_MODULE_LISTENER = 6
  TCP_TABLE_OWNER_MODULE_CONNECTIONS = 7
  TCP_TABLE_OWNER_MODULE_ALL = 8
  
  # for storing socket info python style.  
  class socket_info:
  
      State = None
      LocalAddr = None
      LocalPort = None
      RemoteAddr = None
      RemotePort = None
  
      def __init__ (self, **kwargs):
  
          for key, word in kwargs.items():
              setattr(self, key, word)
  
  def formatip (ip):
      ip = inet_aton (str(ip))
      return inet_ntoa (ip[::-1])
  
  states = {
      1 : "TCP_STATE_CLOSED",
      2 : "TCP_STATE_LISTEN",
      3 : "TCP_STATE_SYN_SENT",
      4 : "TCP_STATE_SYN_RCVD",
      5 : "TCP_STATE_ESTAB",
      6 : "TCP_STATE_FIN_WAIT",
      7 : "TCP_STATE_FIN_WAIT2",
      8 : "TCP_STATE_CLOSE_WAIT",
      9 : "TCP_STATE_CLOSING",
      10 : "TCP_STATE_LAST_ACK",
      11 : "TCP_STATE_TIME_WAIT",
      12 : "TCP_STATE_DELETE_TCB",
  
      "TCP_STATE_CLOSED" : 1,
      "TCP_STATE_LISTEN" : 2,
      "TCP_STATE_SYN_SENT" : 3,
      "TCP_STATE_SYN_RCVD" : 4,
      "TCP_STATE_ESTAB" : 5,
      "TCP_STATE_FIN_WAIT" : 6,
      "TCP_STATE_FIN_WAIT2" : 7,
      "TCP_STATE_CLOSE_WAIT" : 8,
      "TCP_STATE_CLOSING" : 9,
      "TCP_STATE_LAST_ACK" :10,
      "TCP_STATE_TIME_WAIT" : 11,
      "TCP_STATE_DELETE_TCB" : 12 }
  
  class MIB_TCPROW_OWNER_PID(Structure):
      _fields_ = [
          ("dwState", DWORD),
          ("dwLocalAddr", DWORD),
          ("dwLocalPort", DWORD),
          ("dwRemoteAddr", DWORD),
          ("dwRemotePort", DWORD),
          ("dwOwningPid", DWORD)
          ]
  
  
  class MIB_TCPTABLE_OWNER_PID(Structure):
      _fields_ = [
          ("dwNumEntries", DWORD),
          ("MIB_TCPROW_OWNER_PID", MIB_TCPROW_OWNER_PID * 100)
          ]
  
  
  
  def GetExtendedTcpTable (vip=AF_INET):
      table = MIB_TCPTABLE_OWNER_PID ()
      so = sizeof (table)
      size = DWORD (so)
      order = c_int(1)
  
      failure= windll.iphlpapi.GetExtendedTcpTable (
          byref (table),
          addressof (size),
          order,
          vip,
          TCP_TABLE_OWNER_PID_ALL,
          0    )
  
      assert not failure,  WinError (GetLastError ())
  
      pytables = []
      tables = table.MIB_TCPROW_OWNER_PID
  
      for index in range(table.dwNumEntries):
          table = tables [index]
          pytables.append (
              socket_info (
                  State=states.get (table.dwState, "UNKNOWN_STATE_%s" %(str(table.dwState))),
                  LocalAddr=formatip (table.dwLocalAddr),
                  LocalPort=htons(table.dwLocalPort),
                  RemoteAddr=formatip (table.dwRemoteAddr),
                  RemotePort=htons(table.dwRemotePort),
                  OwningPid = int (table.dwOwningPid)
              )
          )
      return pytables
  
  
  def GetTcpTableForPid (pid):
      tables = GetExtendedTcpTable ()
      for table in tables:
          if table.OwningPid == pid: return table
      raise "Cannot find tcp table for pid %s" %pid
  
  dict_process = {}
  pid_set =set()
  pid_list = []
  tcp_info_list = []
  tcp_info = GetExtendedTcpTable()
  for item in tcp_info:
      LocalAddr = item.LocalAddr
      LocalPort = item.LocalPort
      RemoteAddr = item.RemoteAddr
      RemotePort = item.RemotePort
      OwningPid = item.OwningPid
      print('local Addr: '+ LocalAddr,'local port: '+ str(LocalPort),'remote Addr: ' + RemoteAddr, 'Remote Port: ' + str(RemotePort), OwningPid)
  </code></pre>
  
  <p>The script is run from time to time. It can run for 5 minutes and then don't work about an hour with this stupid mistake. How to get around it?</p>
  
  <p>I really dont know, what's with it. Please, help me, what i do wrong?</p>
  
  <p>I use python 3.2 on Win7 SP1 x64</p>
  
  <p>Thank you a lot!</p>