title: Extract Ethernet, IP header, TCP and payload from socket recv Python
body: <p>How can I extract Extract Ethernet, IP header, TCP and payload from <strong>socket.recv</strong> in Python
  Right now I can obtain information above using <strong>socket.recvfrom()</strong>:</p>
  
  <pre><code>    packet = s.recvfrom(NETWORK_MAX_SIZE)
      packet = packet[0] 
      #parse ethernet header
      eth_length = 14
      eth_header = packet[:eth_length]
      eth = unpack('!6s6sH' , eth_header)
      eth_protocol = socket.ntohs(eth[2])
      t = iph_length + eth_length
      tcp_header = packet[t:t+20]
      #now unpack them :)
      tcph = unpack('!HHLLBBHHH' , tcp_header)
      source_port = tcph[0]
      dest_port = tcph[1]
      sequence = tcph[2]
      acknowledgement = tcph[3]
      doff_reserved = tcph[4]
      tcph_length = doff_reserved &gt;&gt; 4
      h_size = eth_length + iph_length + tcph_length * 4
      data_size = len(packet) - h_size
      #get data from the packet
      data = packet[h_size:] 
  </code></pre>
  
  <p>Reference:
  <a href="http://www.binarytides.com/python-packet-sniffer-code-linux/" rel="nofollow">http://www.binarytides.com/python-packet-sniffer-code-linux/</a></p>
  
  <p>When I use same function by a fragmented TCP packet and call <strong>socket.recv()</strong> I get errors when unpacking tcpheader.</p>
  
  <p>Thanks </p>