如何通过公网IP跨网络执行Appium自动化脚本(含跨端场景)
Hey there! Let's tackle your cross-network Appium questions one by one—both scenarios are totally solvable with the right setup.
1. Running automation scripts on a remote Appium server via public IP
If you want to trigger scripts that execute on a remote Appium server with a public IP, follow these steps:
Configure the remote Appium server for public access
- First, open the default Appium port (usually
4723) on the server's firewall. For Ubuntu, run:
If it's a cloud server like AWS or Azure, don't forget to update the security group rules to allow inbound traffic on port 4723.sudo ufw allow 4723/tcp sudo ufw reload - Start the Appium server bound to all network interfaces (not just localhost) so it accepts connections from the public internet:
appium -a 0.0.0.0 -p 4723
- First, open the default Appium port (usually
Update your script's Desired Capabilities
- In your automation code, set the remote URL to point to the server's public IP and port. For example, in Python:
from appium import webdriver desired_caps = { "platformName": "Android", "deviceName": "YourTargetDevice", # Add other required capabilities like app package/activity } driver = webdriver.Remote( command_executor="http://<YOUR_PUBLIC_SERVER_IP>:4723/wd/hub", desired_capabilities=desired_caps ) - Double-check that the device connected to the remote server matches the capabilities you've set (e.g., correct OS version, installed app).
- In your automation code, set the remote URL to point to the server's public IP and port. For example, in Python:
2. Running scripts from remote Ubuntu Server on a local Appium server cross-network
This is the reverse scenario—your scripts live on a cloud Ubuntu server, but you want them to run against a local Appium server on your machine behind a home/office network. Here are three reliable approaches:
Option 1: Use an internal network tunneling tool quick, for testing
Tools like ngrok let you temporarily expose your local Appium server to the public internet:
- On your local machine, start ngrok to forward port 4723:
You'll get a public URL likengrok http 4723https://abc123.ngrok.iothat routes traffic to your locallocalhost:4723. - Start your local Appium server bound to all interfaces:
appium -a 0.0.0.0 -p 4723 - In your Ubuntu server's script, set the remote URL to the ngrok address plus
/wd/hub:driver = webdriver.Remote( command_executor="http://abc123.ngrok.io/wd/hub", desired_capabilities=desired_caps )
Option 2: Set up a reverse proxy for long-term use
If you need a permanent solution, use a public-facing server as a reverse proxy to route traffic to your local Appium server:
- On a server with a public IP, install Nginx and add this configuration to
/etc/nginx/sites-available/appium-proxy:server { listen 4723; server_name <YOUR_PUBLIC_PROXY_IP>; location /wd/hub { proxy_pass http://<YOUR_LOCAL_APPIUM_SERVER_LAN_IP>:4723; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } - Enable the config and restart Nginx:
sudo ln -s /etc/nginx/sites-available/appium-proxy /etc/nginx/sites-enabled/ sudo systemctl restart nginx - Make sure your local network allows inbound traffic from the proxy server either via port forwarding on your router or a VPN connection between the proxy and your local machine.
- Update your Ubuntu script to use the proxy server's public IP:
driver = webdriver.Remote( command_executor="http://<YOUR_PUBLIC_PROXY_IP>:4723/wd/hub", desired_capabilities=desired_caps )
Option 3: Use a VPN most secure
Connect both your local machine and the remote Ubuntu server to the same VPN network like WireGuard or OpenVPN. This puts them in a virtual local network, so you can use your local Appium server's VPN-assigned IP directly:
- Once both devices are on the VPN, get your local machine's VPN IP e.g.,
10.0.0.5. - Start your local Appium server bound to all interfaces:
appium -a 0.0.0.0 -p 4723 - In your Ubuntu script, use the VPN IP as the remote URL:
driver = webdriver.Remote( command_executor="http://10.0.0.5:4723/wd/hub", desired_capabilities=desired_caps )
Key Notes for All Scenarios
- Security first: Add basic authentication to your Appium server to prevent unauthorized access. Start it with:
Then update your script to include credentials in the URL:appium --basic-auth your_username:your_secure_passworddriver = webdriver.Remote( command_executor="http://your_username:your_secure_password@<PUBLIC_IP>:4723/wd/hub", desired_capabilities=desired_caps ) - Check latency: Cross-network connections might have delays—adjust your script's implicit/explicit wait times accordingly to avoid flaky tests.
- Verify device connectivity: Ensure your local/remote device is properly detected by Appium before running scripts use
appium-doctorto troubleshoot.
内容的提问来源于stack exchange,提问作者Sadha Nanda

