需要帮助在Python中创建原始套接字

时间:2013-10-05 17:53:57

标签: python sockets python-2.7

我正在使用Python 2.7。我已经尝试了一切,但它没有连接。我得到的错误是:

Socket could not be created. Error Code : 10013 Message An attempt was 
    made to access a socket in a way forbidden by its access permissions.

我正在打乒乓球。

def doOnePing(destAddr, timeout):
    icmp = socket.getprotobyname("icmp")
    # SOCK_RAW is a powerful socket type. For more details see: 
    # http://sock-raw.org/papers/sock_raw
    # Fill in start
    # Create Socket here
    try:
        #mySocket = socket.socket(2, 3, 1)
        # the public network interface
        HOST = socket.gethostbyname(socket.gethostname())

        # create a raw socket and bind it to the public interface
        mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
        mySocket.bind((HOST, 0))

        # Include IP headers
        mySocket.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

        # receive all packages
        mySocket.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

        # receive a package
        print mySocket.recvfrom(65565)

        print ('Connected by', destAddr)
    except socket.error , msg:
        print ('Socket could not be created. Error Code : ' + str(msg[0]) + 
              ' Message ' + msg[1])
    #Fill in end

1 个答案:

答案 0 :(得分:1)

尝试以特权用户身份运行脚本。 Linux上的root用户是合适的。