Ollama Python客户端自定义Host遇httpx.ConnectError连接拒绝问题
解决Ollama Python客户端自定义Host连接拒绝问题
问题现象
使用Ollama Python客户端设置自定义Host(非默认11434端口)时,无论通过Client(host='http://localhost:11435')参数还是OLLAMA_HOST环境变量配置,均触发连接拒绝错误:
- Linux系统报错:
httpx.ConnectError: [Errno 111] Connection refused - Windows系统报错:
httpx.ConnectError: [错误 10061] 由于目标计算机积极拒绝,无法连接
问题最初在LangChain Python版中发现,经排查根源为ollama-python库的连接配置问题。
复现代码
Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from ollama import Client >>> client = Client(host='http://localhost:11435') >>> response = client.chat(model='llama3.1', messages=[ ... { ... 'role': 'user', ... 'content': 'Why is the sky blue?', ... }, ... ])
完整报错栈
Traceback (most recent call last): File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 72, in map_httpcore_exceptions yield File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 236, in handle_request resp = self._pool.handle_request(req) File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 216, in handle_request raise exc from None File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 196, in handle_request response = connection.handle_request( File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 99, in handle_request raise exc File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 76, in handle_request stream = self._connect(request) File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 122, in _connect stream = self._network_backend.connect_tcp(**kwargs) File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_backends/sync.py", line 205, in connect_tcp with map_exceptions(exc_map): File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__ self.gen.throw(typ, value, traceback) File "/home/bermudez/.local/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions raise to_exc(exc) from exc httpcore.ConnectError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/bermudez/.local/lib/python3.10/site-packages/ollama/_client.py", line 236, in chat return self._request_stream( File "/home/bermudez/.local/lib/python3.10/site-packages/ollama/_client.py", line 99, in _request_stream return self._stream(*args, **kwargs) if stream else self._request(*args, **kwargs).json() File "/home/bermudez/.local/lib/python3.10/site-packages/ollama/_client.py", line 70, in _request response = self._client.request(method, url, **kwargs) File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_client.py", line 837, in request return self.send(request, auth=auth, follow_redirects=follow_redirects) File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_client.py", line 926, in send response = self._send_handling_auth( File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_client.py", line 954, in _send_handling_auth response = self._send_handling_redirects( File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_client.py", line 991, in _send_handling_redirects response = self._send_single_request(request) File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_client.py", line 1027, in _send_single_request response = transport.handle_request(request) File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 235, in handle_request with map_httpcore_exceptions(): File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__ self.gen.throw(typ, value, traceback) File "/home/bermudez/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 89, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.ConnectError: [Errno 111] Connection refused
解决方案
1. 确认Ollama服务在指定端口运行
连接拒绝的核心原因通常是Ollama服务并未在你指定的11435端口监听,需先确保服务端配置正确:
- Linux/macOS:启动Ollama时指定端口:
OLLAMA_HOST=0.0.0.0:11435 ollama serve - Windows:在系统环境变量中添加
OLLAMA_HOST值为http://localhost:11435,然后重启Ollama服务(可通过任务管理器结束ollama.exe后重新启动)
验证端口监听状态:
- Linux:
ss -tulpn | grep 11435 - Windows:
netstat -ano | findstr :11435
2. 正确配置Python客户端
- 确保
host参数为完整URL格式(包含http://或https://):from ollama import Client client = Client(host='http://localhost:11435') - 若使用环境变量配置,需在导入ollama模块前设置,否则配置不会生效:
import os os.environ['OLLAMA_HOST'] = 'http://localhost:11435' from ollama import Client # 必须在环境变量设置后导入 client = Client()
3. 排查网络权限
- 检查防火墙是否允许11435端口的入站连接:
- Linux:临时关闭防火墙测试
sudo ufw disable(测试完成后重新开启sudo ufw enable) - Windows:在「Windows Defender防火墙」→「高级设置」中添加11435端口的允许入站规则
- Linux:临时关闭防火墙测试
内容的提问来源于stack exchange,提问作者tdimitch
相关产品推荐
相关产品推荐

