Python Forum
struct.error: unpack requires a buffer of 14 bytes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
struct.error: unpack requires a buffer of 14 bytes
#1
Hi,
I am trying to read data from a socket and unpack it. But I get this error while running the code.

import socket as socket_mod
import struct
import textwrap

def main():
    conn=socket_mod.socket(socket_mod.AF_INET, socket_mod.SOCK_STREAM)
    conn.connect(("147.83.49.195",7))
    while True:
        raw_data, addr=conn.recvfrom(65536)
        print(raw_data)
        print(addr)
        dest, src, eth_proto, data=ethernet_frame(raw_data)
        print('\n ethernet frame:')
        print('dest: {}, src: {}, protocol: {}'.format(dest, src, eth_proto))

def ethernet_frame(data):
    dest, src, proto = struct.unpack('= 6s 6s H', data[:14])
    return get_mac_addr(dest), get_mac_addr(src), socket_mod.htons(proto), data[14:]


def get_mac_addr(bytes_addr):
    bytes_str= map('{:02x}'.format, bytes_addr)
    return ':'.join(bytes_str).upper()


main()
Error:
Traceback (most recent call last): File "eth3.py", line 26, in <module> main() File "eth3.py", line 12, in main dest, src, eth_proto, data=ethernet_frame(raw_data) File "eth3.py", line 17, in ethernet_frame dest, src, proto = struct.unpack('= 6s 6s H', data[:14]) struct.error: unpack requires a buffer of 14 bytes
I get this error although I removed the padding.
Any suggestions to overcome this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getting wrong source IP address using socket and struct billykid999 1 1,297 May-25-2023, 08:29 PM
Last Post: billykid999
  UDP Socket: How to clear the buffer and ignore oldes messages Guybrush 3 28,118 Apr-26-2017, 02:06 PM
Last Post: Guybrush
  How to use fcntl.ioctl() to get output buffer of socket after sendall() operation apsaxena 0 4,719 Dec-09-2016, 12:44 PM
Last Post: apsaxena

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020