使用Python脚本通过WebSocket连接OBS失败,出现目标机器主动拒绝连接错误
Let's break down why you're hitting that "No connection could be made because the target machine actively refused it" error, and walk through fixes step by step:
1. OBS WebSocket Server Isn't Enabled (or Mismatched Settings)
This is the most common culprit. Even if OBS is open, if the WebSocket server isn't turned on, your script can't connect.
- Open OBS, go to Tools > WebSocket Server Settings
- Make sure the box for "Enable WebSocket server" is checked
- Verify the Port matches what's in your code (you're using 4455—double-check it's identical in OBS settings)
- If you set a password in OBS, your script's
passwordparameter needs to match exactly. Since your code uses an empty string, leave the password field blank in OBS settings too.
2. OBS Isn't Fully Loaded When Your Script Tries to Connect
Your time.sleep(5) might not be enough—OBS can take longer to start up, especially if it's loading plugins, scenes, or large sources. If the WebSocket server hasn't finished initializing yet, your script will get a connection refused error.
- Quick fix: Increase the sleep time to 10-15 seconds and test again.
- Better fix: Add retry logic to avoid relying on hardcoded delays. Here's how to modify your connection code:
client = None max_retries = 5 retry_delay = 3 # Seconds between retries for attempt in range(max_retries): try: client = obs.ReqClient(host='localhost', port=4455, password="", timeout=3) print("Connected to OBS successfully!") break except ConnectionRefusedError: print(f"Connection attempt {attempt+1} failed. Retrying in {retry_delay} seconds...") time.sleep(retry_delay) else: print("Failed to connect to OBS after multiple attempts.") exit(1) # Rest of your code here obs_version = client.get_version() print(f"OBS version: {obs_version.version}") client.set_current_program_scene("clip paint") client.start_record() client.disconnect()
3. Firewall/Antivirus Is Blocking the Connection
Local firewalls or antivirus tools sometimes block connections to localhost ports, even when it's your own script talking to OBS.
- Temporarily disable your firewall/antivirus and test the script. If it works, add an exception for both
obs64.exeand your Python executable to allow inbound/outbound connections on port 4455.
4. Version Mismatch Between obsws_python and OBS WebSocket
OBS 28+ includes WebSocket 5.x by default. If your obsws_python package is outdated, it might not support the newer protocol.
- Run this command to update the package:
pip install --upgrade obsws_python - If you're using an older OBS version (pre-28), you'll need to install the OBS WebSocket plugin manually and ensure it's compatible with your
obsws_pythonversion.
5. Port Conflict
Rarely, another program might be using port 4455, preventing OBS from starting its WebSocket server.
- Check if port 4455 is in use by running this in Command Prompt:
netstat -ano | findstr :4455 - If you see a process ID (PID) listed, either close that program or change the WebSocket port in OBS settings (and update your code to match the new port).
内容的提问来源于stack exchange,提问作者cooky

