python 嗅探器报错[Errno 10013]

import socket
import os
host = "192.168.10.147"

if os.name == 'nt':
socket_protocol = socket.IPPROTO_IP
else:
socket_protocol = socket.IPPROTO_ICMP
sniffer = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket_protocol)
sniffer.bind((host,9999))
sniffer.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL, 1)_

if os.name == "nt":
sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_ON)

print sniffer.recvfrom(65565)

if os.name == 'nt':
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

运行结果:
Traceback (most recent call last):
0
File "C:/Users/Think/PycharmProjects/Test/sniffer.py", line 10, in
sniffer = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket_protocol)
File "C:\Python27\lib\socket.py", line 191, in init
_sock = _realsocket(family, type, proto)
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
不是端口的问题,在获取sniffer实例的时候就已经异常了。是操作权限的问题吗?

1个回答

你这使用的是底层套接字,需要用root权限,或者管理员权限执行你的py脚本

snowyli_1989
snowyli_1989 确实是权限的问题,虽然我已经把当前用户设置为administrator了,但是,依然报错,切换到Administrator用户下就可以了。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问