获取计算机的host和port可以通过以下方法:
通过配置文件读取
如果你有一个INI格式的配置文件,如`config.ini`,你可以使用Python的`configparser`模块来读取其中的host和port信息。例如:
```python
import configparser
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')
db_host = config.get('Database', 'host')
db_port = config.getint('Database', 'port')
print(f"Database host: {db_host}")
print(f"Database port: {db_port}")
```
通过命令行查看
Windows系统
1. 打开命令提示符(按下Win + R键,输入`cmd`并按下回车)。
2. 输入`ipconfig`命令,查看IPv4地址(即本机IP地址)。
3. 输入`netstat -a`命令,查看所有正在使用的端口及其对应的进程。
Linux系统
1. 打开终端。
2. 输入`ifconfig`命令,查看网络接口的IP地址。
3. 输入`netstat -tunlp`命令,查看所有正在使用的端口及其对应的进程。
通过编程语言获取
如果你使用的是Python,可以通过`socket`库获取当前服务器的地址和端口。例如:
```python
import socket
host = socket.gethostname()
port = socket.getservbyname(socket.gethostname(), 'http')
print(f"Host: {host}")
print(f"Port: {port}")
```
通过系统设置查看
Windows系统
1. 打开系统设置(按下Windows + I快捷键)。
2. 选择“网络和 Internet”选项。
3. 选择当前的网络连接,查看IPv4地址。
Linux系统
1. 打开终端。
2. 输入`ifconfig`命令,查看网络接口的IP地址。
总结
配置文件:使用`configparser`读取INI文件。
命令行:Windows使用`ipconfig`和`netstat`,Linux使用`ifconfig`和`netstat`。
编程语言:使用`socket`库获取当前服务器的地址和端口。
系统设置:在Windows中通过“网络和 Internet”设置查看,在Linux中通过`ifconfig`查看。
选择哪种方法取决于你的具体需求和操作习惯。