如何在python中设置套接字选项IP_DONTFRAG?

时间:2018-03-01 14:13:45

标签: python sockets setsockopt ip-protocol

如何使用python套接字在IP头中设置DONT_FRAGMENT标志?

以下代码

socket.setsockopt(socket.IPPROTO_IP, socket.IP_DONTFRAG, 1)

给了我这个错误:

AttributeError: 'module' object has no attribute 'IP_DONTFRAG'

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

def create_sender_session(self):
    logging.debug("Create Sender Session")
    if (self.send_ip_ver == 6 or self.resp_ip_ver == 6):
        self.sender_socket = socket.socket(
            socket.AF_INET6, socket.SOCK_DGRAM)
        self.sender_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_TCLASS, self.tos)
        self.sender_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_UNICAST_HOPS, self.ttl)
    else:
        self.sender_socket = socket.socket(
            socket.AF_INET, socket.SOCK_DGRAM)
        self.sender_socket.setsockopt(
            socket.SOL_IP, socket.IP_TTL, self.ttl)
        self.sender_socket.setsockopt(
            socket.IPPROTO_IP, socket.IP_TOS, self.tos)
    self.sender_socket.setsockopt(
        socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sender_socket.bind((self.send_addr, self.send_port))
    logging.info('Sender started: {}:{}'.format(
        self.send_addr, self.send_port))