1
我工作的一個程序在Python和所有的代碼看起來當我運行該程序從終端我收到以下錯誤消息,除了良好:未知格式代碼「X」
Traceback (most recent call last):
File "packetSniffer.py", line 25, in <module>
main()
File "packetSniffer.py", line 10, in main
reciever_mac, sender_mac, ethernetProtocol, data = ethernet_frame(rawData)
File "packetSniffer.py", line 17, in ethernet_frame
return getMacAddress(reciever_mac), getMacAddress(sender_mac), socket.htons(protocol), data[14:]
File "packetSniffer.py", line 21, in getMacAddress
bytesString = map('{:02x}'.format, bytesAddress)
ValueError: Unknown format code 'x' for object of type 'str'
這裏是到目前爲止我的整個程序的代碼,任何人都可以提供幫助嗎?
import struct
import textwrap
import socket
def main():
connection = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
rawData, address = connection.recvfrom(65535)
reciever_mac, sender_mac, ethernetProtocol, data = ethernet_frame(rawData)
print('\nEthernet Frame: ')
print('Destination: {}, Source: {}, Protocol: {}'.format(reciever_mac, sender_mac, ethernetProtocol))
# Unpack ethernet frame
def ethernet_frame(data):
reciever_mac, sender_mac, protocol = struct.unpack('! 6s 6s H', data[:14])
return getMacAddress(reciever_mac), getMacAddress(sender_mac), socket.htons(protocol), data[14:]
# Convert the Mac address from the jumbled up form from above into human readable format
def getMacAddress(bytesAddress):
bytesString = map('{:02x}'.format, bytesAddress)
macAddress = ':'.join(bytesString).upper()
return macAddress
main()
你確定這是Python 3嗎? – Will
似乎是從這裏:https://github.com/koehlma/snippets/blob/master/python/network/sniffer.py https://stackoverflow.com/questions/34968090/python-3-4-未知格式碼-X – Will