Help Center> > Developer Guide > Python >Anti-DDoS Python SDK Demo

Anti-DDoS Python SDK Demo

Querying Optional Anti-DDoS Defense Policies

You can query optional Anti-DDoS defense policies. Based on your service, you can select a policy for Anti-DDoS traffic cleaning.

Code example:

def list_config(conn):
    print("list anti-ddos confit")
print(conn.anti_ddos.query_config_list())

Enabling Anti-DDoS

You can enable Anti-DDoS traffic cleaning defense. Successfully invoking this API only means that the service node has received the enabling request. You need to use the task querying API to check the task execution status.

Code example:

def create_eip(conn):
    fip_dict = {'enable_L7': True,
                'traffic_pos_id': 1,
                'http_request_pos_id': 1,
                'cleaning_access_pos_id': 1,
                'app_type_id': 0}

    fip = conn.anti_ddos.create_floating_ip(FLOATING_IP_ID, **fip_dict)
print(fip)

Disabling Anti-DDoS

You can disable Anti-DDoS traffic cleaning defense. Successfully invoking this API only means that the service node has received the disabling request. You need to use the task querying API to check the task execution status.

Code example:

def delete_eip(conn):
    fip = conn.anti_ddos.get_floating_ip(FLOATING_IP_ID)
    conn.anti_ddos.delete_floating_ip(fip)

Querying Configured Anti-DDoS Defense Policies

You can query the configured Anti-DDoS defense policy of a specified EIP.

Code example:

def get_eip(conn):
    fip = conn.anti_ddos.get_floating_ip(FLOATING_IP_ID)
    print(fip)

Updating Anti-DDoS Defense Policies

You can update the Anti-DDoS defense policy of a specified EIP. Successfully invoking this API only means that the service node has received the update request. You need to use the task querying API to check the task execution status.

Code example:

def update_eip(conn):
    fip = conn.anti_ddos.get_floating_ip(FLOATING_IP_ID)

    fip_update_dict = {'enable_L7': False,
                       'traffic_pos_id': 1,
                       'http_request_pos_id': 1,
                       'cleaning_access_pos_id': 1,
                       'app_type_id': 0}
    ufip = conn.anti_ddos.update_floating_ip(fip, **fip_update_dict)
    print(ufip)

Querying Anti-DDoS Tasks

You can query the execution status of a specified Anti-DDoS configuration task.

Code example:

def query_task_status(conn):
    print(conn.anti_ddos.query_task_status(
        '228186d4-4aec-4c37-bae8-cb025aaf5770'))

Querying the List of EIP Defense Statuses

You can query the defense statuses of all EIPs, regardless whether an EIP has been bound to an ECS or not.

Code example:

def list_eips(conn):
    print("list eips by status")
    for l in conn.anti_ddos.floating_ips():
    print(l)

Querying the Defense Status of a Specified EIP

You can query the defense status of a specified EIP.

Code example:

def get_eip_status(conn):
    print(conn.anti_ddos.get_eip_status(FLOATING_IP_ID))

Querying the Traffic of a Specified EIP

You can query the traffic of a specified EIP in the last 24 hours. Traffic is detected at five-minute intervals.

Code example:

def get_eip_daily(conn):
    for d in conn.anti_ddos.list_eip_daily(FLOATING_IP_ID):
        print(d)

Querying Events of a Specified EIP

You can query events of a specified EIP in the last 24 hours. Events include cleaning and blackhole events, and the query delay is within five minutes.

Code example:

def get_eip_log(conn):
    for l in conn.anti_ddos.list_eip_log(FLOATING_IP_ID):
        print(l)

Querying Weekly Defense Statistics

You can query weekly defense statistics about all your EIPs, including the number of intercepted DDoS attacks, number of attacks, and ranking by the number of attacks.

Code example:

def get_eip_weekly(conn):
    print(conn.anti_ddos.get_eip_weekly('1006510306'))

Querying Alarm Configuration

You can query alarm configuration, such as whether a certain type of alarms will be received, and whether alarms are received through SMS messages or emails.

Code example:

def get_alert_config(conn):
    print(conn.anti_ddos.get_alert_config())