获取本机地址列表之Unicode版本
时间: 2023-08-03 16:05:18 浏览: 164
获取本机IP地址的代码
获取本机地址列表的Unicode版本可以使用Python的socket模块和unicodedata模块。下面是一个示例代码:
```python
import socket
import unicodedata
# 获取本机地址列表
addrs = socket.getaddrinfo(socket.gethostname(), None)
# 遍历地址列表并输出
for addr in addrs:
# 将地址元组转换为Unicode字符串
addr_str = unicodedata.normalize('NFKD', str(addr)).encode('ascii', 'ignore').decode('utf-8')
print(addr_str)
```
这段代码会输出本机所有的IP地址和端口号,例如:
```
(2, 1, 6, '', ('127.0.0.1', 0))
(2, 2, 17, '', ('fe80::1%lo0', 0, 1, 1))
(2, 1, 2, '', ('127.0.0.1', 65432))
(2, 2, 17, '', ('::1%lo0', 0, 1, 1))
(30, 0, 6, '', ('192.168.1.101', 0))
(30, 0, 17, '', ('fe80::b49e:b7ff:fedf:cf8d%en0', 0, 18, 1))
(30, 0, 6, '', ('192.168.56.1', 0))
(30, 0, 17, '', ('fe80::d2a8:5eff:fe25:9c3a%utun1', 0, 7, 1))
(2, 3, 0, '', ('0.0.0.0', 0))
(2, 3, 0, '', ('::', 0, 0, 0))
```
阅读全文