Python Raw Socket to Ethernet Interface (Windows)

后端 未结 5 739
滥情空心
滥情空心 2021-01-15 06:14

I\'m trying to create a DHCP Server and the first step is for me to send packets through my ethernet port. I\'m trying to send packets to my Ethernet interface and having an

5条回答
  •  太阳男子
    2021-01-15 06:35

    This example from the docs seems instructive. https://docs.python.org/2/library/socket.html

    import socket
    
    # the public network interface
    HOST = socket.gethostbyname(socket.gethostname())
    
    # create a raw socket and bind it to the public interface
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
    s.bind((HOST, 0))
    
    # Include IP headers
    s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
    
    # receive all packages
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
    
    # receive a package
    print s.recvfrom(65565)
    
    # disabled promiscuous mode
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
    

    I think the key is socket.gethostbyname(socket.gethostname()). "eth0" as used in your example won't be supported on Windows.

提交回复
热议问题