【发布时间】:2026-02-15 07:30:02
【问题描述】:
我可以在 linux 上运行脚本,但在同一台机器上,如果 apache 运行脚本,我会得到 TimeoutException. Message: connection refused
查看 geckodriver.log,它说:
1604976847149 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilehZbDYR"
Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Running without a11y support!
*** You are running in headless mode.
我认为这部分只是一个警告 - 不是错误?我不确定。
要启动 Firefox,我基本上是这样做的:https://github.com/timgrossmann/InstaPy/blob/master/instapy/browser.py
不使用 XVFB 或任何虚拟显示器,仅使用“-headless”和 Firefox 扩展。
知道为什么这段代码可以在命令行上完美运行,但如果从 apache 调用就不行?
这是完整的跟踪:
TimeoutException: Message: connection refused
File "django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "mainapp/views.py", line 277, in api
send_message(message, recipient_username)
File "scripts/browser_login.py", line 434, in send_message
driver = browser_login(username, password)
File "scripts/browser_login.py", line 123, in browser_login
options=firefox_options,
File "selenium/webdriver/firefox/webdriver.py", line 162, in __init__
keep_alive=True)
File "selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
这恰好在脚本启动 60 秒后发生。它位于 webdriver 初始化。
【问题讨论】:
标签: selenium firefox geckodriver