title: Packet sniffer in Python
body: <p>I want to do a packet sniffer in Python 3.5 which captures UDP, TCP and ICMP. This is a short example of it:</p>
  
  <pre><code> import socket
   import struct
  
  # the public network interface
  HOST = socket.gethostbyname(socket.gethostname())
  # create a raw socket and bind it to the public interface
  s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
  
  s.bind((HOST,0))
  
  # Include IP headers
  s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  
  # receive all packages
  s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  
  # receive a package
  n=1
  while(n&lt;=400):
      print('Number ', n)
      data=s.recvfrom(65565)
      packet=data[0]
      address= data[1]
      header=struct.unpack('!BBHHHBBHBBBBBBBB', packet[:20])
      if(header[6]==6): #header[6] is the field of the Protocol
          print("Protocol = TCP")
      elif(header[6]==17):
          print("Protocol = UDP")
      elif(header[5]==1):
          print("Protocol = ICMP") 
      n=n+1
  </code></pre>
  
  <p>The problem is that it only captures UDP packets :( 
  Output:</p>
  
  <pre><code>Number  1 Protocol = UDP Number  2 Protocol = UDP Number  3 Protocol = UDP Number  4 Protocol = UDP Number  5 Protocol = UDP Number  6 Protocol = UDP Number  7
  </code></pre>
  
  <p>There are 2 options:</p>
  
  <ul>
  <li>The sniffer can only capture UDP packets.</li>
  <li>I'm just receiving UDP packets.</li>
  </ul>
  
  <p>I think that the most logical answer is my sniffer doesn't work correctly and it's just capturing UDP. Any idea?</p>