# RuntimeError: 连接被拒绝：服务器未运行或无法访问。

- **ID:** `python/starlette-runtime-error-connection-refused`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试连接到未启动或正在侦听不同主机/端口的 Starlette 服务器。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Start server with correct host and port** (90% 成功率)
   ```
   uvicorn app:app --host 0.0.0.0 --port 8000
# Then connect to http://localhost:8000
   ```
2. **Check firewall and network settings** (80% 成功率)
   ```
   sudo ufw status
# Allow port if needed: sudo ufw allow 8000
   ```

## 无效尝试

- **Checking only localhost without specifying port** — Server may be on a different port or bound to 0.0.0.0. (70% 失败率)
- **Using wrong protocol (http vs https)** — If server uses SSL, http connections fail. (60% 失败率)
