Apr-18-2018, 11:06 AM
Hi,
I am trying to read data from a socket and unpack it. But I get this error while running the code.
Any suggestions to overcome this?
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?