You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Appium-Python连接Android模拟器遇连接拒绝错误求助

问题描述

使用Appium-Python代码连接Android模拟器时,遇到以下错误:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=4723): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd691cf0af0>: Failed to establish a new connection: [Errno 111] Connection refused'))

尝试通过Appium建立与Android模拟器的连接,但连接被拒绝,请求重试次数已达上限。

环境信息
  • Windows 11操作系统
  • Pycharm Professional IDE
  • Android 11版本模拟器
  • 官方网站下载的最新版Appium
Appium日志

Connected to pydev debugger (build 223.8617.48)
Traceback (most recent call last):
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 398, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connection.py", line 239, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/home/mohanraj/.pyenv/versions/3.9.16/lib/python3.9/http/client.py", line 1285, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/home/mohanraj/.pyenv/versions/3.9.16/lib/python3.9/http/client.py", line 1331, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/home/mohanraj/.pyenv/versions/3.9.16/lib/python3.9/http/client.py", line 1280, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/mohanraj/.pyenv/versions/3.9.16/lib/python3.9/http/client.py", line 1040, in _send_output
self.send(msg)
File "/home/mohanraj/.pyenv/versions/3.9.16/lib/python3.9/http/client.py", line 980, in send
self.connect()
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connection.py", line 205, in connect
conn = self._new_conn()
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fd691cf0af0>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/connectionpool.py", line 787, in urlopen
retries = retries.increment(
File "/home/mohanraj/.cache/pypoetry/virtualenvs/mis-automation-L9yvBObc-py3.9/lib/python3.9/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=4723): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd691cf0af0>: Failed to establish a new connection: [Errno 111] Connection refused'))
python-BaseException

Process finished with exit code 1

复现代码
from time import sleep

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

desired_caps = {
    "deviceName": "Pixel 4a API 30",
    "platformName": "Android",
    "udid": "emulator-5554",
    "version": 11,
    "app": "C:\\Users\\lenovo\\Downloads\\com.indigo.new.apk"
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

解决方法

这个错误核心是Python代码无法连接到本地4723端口的Appium服务,按以下步骤排查:

  1. 确认Appium服务已启动
    打开Appium Desktop,点击"Start Server"按钮,确保控制台显示类似[Appium] Welcome to Appium vX.X.X和[Appium] Appium REST http interface listener started on 0.0.0.0:4723的日志,说明服务正常运行。

  2. 检查端口占用情况
    打开Windows命令提示符,执行netstat -ano | findstr :4723,如果输出中没有Appium相关进程ID,说明端口未被占用;如果有其他进程占用,要么关闭该进程,要么修改Appium监听端口(在Appium设置里修改端口号,同时代码中webdriver.Remote的URL也要对应修改)。

  3. 验证模拟器连接状态
    打开命令提示符,执行adb devices,确保输出中显示emulator-5554设备状态为device。如果显示offline,重启模拟器后再执行命令。

  4. 修正Desired Capabilities参数
    你的desired_caps里有个小问题:version参数应该改为platformVersion,Appium官方规范中使用platformVersion指定Android版本,修改后如下:

    desired_caps = {
        "deviceName": "Pixel 4a API 30",
        "platformName": "Android",
        "udid": "emulator-5554",
        "platformVersion": "11",
        "app": "C:\\Users\\lenovo\\Downloads\\com.indigo.new.apk"
    }
    
  5. 检查防火墙设置
    确保Windows防火墙没有阻止Appium服务的4723端口,或者临时关闭防火墙测试连接是否正常。

  6. 重启相关服务
    关闭Appium服务、模拟器和Pycharm,重新启动后再运行代码,排除临时进程冲突问题。


内容的提问来源于stack exchange,提问作者Mohanraj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 20:05:12