繁体   English   中英

AttributeError:模块'socket'没有属性'AF_PACKET'

[英]AttributeError: module 'socket' has no attribute 'AF_PACKET'

我正在使用Python构建数据包嗅探程序,但是我已经遇到了速度提升。 出于某种原因,我认为socket没有正确导入,因为我在运行程序时收到以下消息: AttributeError: module 'socket' has no attribute 'AF_PACKET'

我正在使用OS X和Pycharm是我的IDE,如果有帮助我运行最新版本的Python。

无论如何,到目前为止我的完整程序是:

import struct
import textwrap
import socket

def main():
    connection = socket.socket(socket.AF_PACKET, socket.SOCKET_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(socket), 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()

在此先感谢您的帮助!

实际上,AF_PACKET不适用于OS X,它适用于Linux。

Mac OS X(Darwin)下的AF_PACKET等价物

我在macOS 10.13.1上使用Python 3.6.3和这个与python3兼容的scapy fork遇到了这个问题。

我使用的是该工具的0.22版,并且正如本期中所建议的降级到版本0.21修复了这个问题!

如果scapy不是一个可行的选择,你也可以按照这篇文章的建议尝试pcap库(虽然这里似乎需要使用python 2)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM