查询ip代码
时间: 2023-07-02 19:08:44 浏览: 45
查询IP地址的代码可以使用Python的requests库和ipapi.co API。以下是一个例子:
```python
import requests
ip_address = "8.8.8.8" # IP地址,可以替换成你要查询的IP地址
url = f"https://ipapi.co/{ip_address}/json/"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(f"IP地址: {data['ip']}")
print(f"所在地: {data['city']}, {data['region']}, {data['country']}")
print(f"经纬度: {data['latitude']}, {data['longitude']}")
else:
print("查询失败")
```
这个例子使用了ipapi.co API来获取IP地址的信息。你需要先注册一个免费的API密钥,然后将其替换到url中。
相关问题
python ip 查询代码
你可以使用 Python 中的 socket 模块来查询 IP。以下是一个示例代码:
```python
import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")
```
该代码将打印出主机名和 IP 地址。请注意,此代码将返回本地计算机的 IP 地址。如果你想查询其他计算机的 IP 地址,则需要在 `gethostbyname` 函数中提供该计算机的名称或 IP 地址。
查询ip的python代码
您可以使用Python的requests库和ipip.net提供的API来查询IP地址的详细信息。以下是示例代码:
```python
import requests
ip = '1.1.1.1' # 要查询的IP地址
url = f'http://ipapi.ipip.net/find?addr={ip}'
response = requests.get(url)
if response.status_code == 200:
result = response.json()
print(result)
else:
print('查询失败')
```
执行后,将会输出查询到的IP地址的详细信息。注意:您需要在ipip.net网站上免费申请API Key并将其替换到代码中的url参数中。