python - socket.error[Errno 99] 无法分配请求的地址

标签 python sockets

昨天,我创建了一个名为 sniffer_ip_header_decode.py 的 python 脚本。 ,但我有以下错误:

Traceback (most recent call last):
  File "sniffer_ip_header_decode.py", line 51, in <module>
    sniffer.bind((host, 0))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address

这是代码:
import socket
import os
import struct
from ctypes import *

#host to listen on
host = "192.168.0.187"

#our IP header
class IP(Structure): 
    fields = [
       ("ihl",            c_ubyte, 4),
       ("version",        c_ubyte, 4),
       ("tos",            c_ubyte),
       ("len",            c_ushort),
       ("id",             c_ushort),
       ("offset",         c_ushort),
       ("ttl",            c_ubyte),
       ("protocol_sum",   c_ubyte),
       ("sum",            c_ushort),
       ("src",            c_ulong),
       ("dst",            c_ulong),
    ]

    def __new__(self, socket_buffer=None): 
        return self.from_buffer_copy(socket_buffer)

    def __init__(self, socket_buffer=None):  

        #map protocol constants to theyr names
        self.protocol_map = {1:"ICMP", 6:"TCP", 17:"UDP"}

        #Human readable IP addresses
        self.src_address = socket.inet_ntoa(struct.pack("<L",self.src))
        self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst))

        #Human readable protocol
        try: 
            self.protocol = self.protocol_map[seld.protocol_num]
        except: 
            self.protocol = str(self.protocol_num)

#This should look familiar from the previous example
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, 0))
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

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

try: 

    while True: 
        #read in a packet
        raw_buffer = sniffer.recvfrom(65565)[0]

        #create an IP header from the first 20 bytes of the buffer
        ip_header = IP(raw_buffer[0:20])

        #print out the protocol that was detected and the host
        print "Protocol: %s %s -> %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)
#Handle CTRL - C
except KeyboardInterrupt: 

    #if we're using windows. turn off promiscuous mode
    if os.name == "nt": 
        sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)   

最佳答案

这一行是错误的:

host = "192.168.0.187"

变量“host”必须是一个有效的IP,你可以使用你的IP。

关于python - socket.error[Errno 99] 无法分配请求的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43460287/

相关文章:

python - 将 NumPy 二维阵列中的所有二维点连接为三角形网格

android - Python 使用 ApkTool 和 Subprocess 反编译 APK

python - 在多索引 Pandas Dataframe 中设置值的正确方法

python - 安装Python包将会下载、安装和删除其他包

node.js - socket.io 错误 - Web 套接字连接在建立连接之前关闭

javascript - 有没有办法通过浏览器向客户端发送TCP请求?

java - 为什么我不能不等待就将 int 从 java 发送到 C?

python - 在 python 中找到欧氏距离的最快方法

angularjs - 连接到 Node REST api 的应用程序被客户防火墙阻止

http - 套接字连接是否比 Blackberry 上的 http 更快?