This topic shows you how to use an OpenAPI to query the region of an asset by its public IP address.
View the OpenAPI documentation
The API overview explains that you can use the DescribeIpLocationService OpenAPI to query the region of an asset by its public IP address. Review the documentation to learn about the required data and permissions.
Create a RAM user and grant permissions
Identity
You can call this API by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information about the differences between these identities, see Identity, credential, and authorization. In this example, we use a RAM user to make the API call.
Log on to the RAM console to create a RAM user with permissions for API calls only, and record its AccessKey. For more information, see Create a RAM user.
Authorization
Go to the Users page in the RAM console. Find the target RAM user and click Add Permissions in the Actions column.
In the Policy section, search for
Yundunddosand select theAliyunYundunDDosFullAccesspolicy.NoteThe system policies for Anti-DDoS Basic are as follows:
AliyunYundunDDosFullAccess: Grants permissions to manage Anti-DDoS Basic.AliyunYundunDDosReadOnlyAccess: Grants read-only permissions for Anti-DDoS Basic.
Click OK to complete the authorization.
Credential
An AccessKey is automatically generated when you create a RAM user. You can use this AccessKey directly. Alternatively, on the Authentication tab of the RAM user details page, click Create AccessKey to create a new one. For more information, see Create an AccessKey.
Call the OpenAPI
This section shows you how to call the OpenAPI using Python.
Set up the Python environment
Download and install Python.
To download Python 3, visit Python 3.
Check your Python version.
For Linux and macOS
Open a terminal and run
python --version.For Windows
In the Python installation path, double-click python.exe.
Open Command Prompt.
Press Win+R to open the Run dialog box, enter python, and then click OK.
Configure environment variables
To avoid the security risk of hard-coding your AccessKey in the code, this example uses environment variables to store the key. The method for configuring environment variables varies by operating system. For more information, see Configure environment variables on Linux, macOS, and Windows.
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_antiddos_public20170518==1.0.2
pip install alibabacloud_tea_consoleDownload the sample code
In OpenAPI Explorer, go to the DescribeIpLocationService API page by using the call link.
Set the InternetIp parameter to 47.110.XX.XX and click Initiate Call.
Click the SDK Sample Code tab, and then click the Python tab.
Click Download Project to download the sample code package.
Extract the sample code package and navigate to the alibabacloud_sample directory.
Run the code
Run the following command:
python sample.pyThe following output is returned:
{
"Instance":{
"InstanceId":"eip-bp1bxkujsn1kwo4la****",
"InstanceType":"eip",
"InternetIp":"47.110.XX.XX",
"Region":"cn-hangzhou"
},
"RequestId":"377E4E81-C897-5988-9E61-E51A2262E5C1"
}