Skip to content

Internet access

Every sandbox has access to the internet and can be reached by a public URL.

Every sandbox has a public URL that can be used to access running services inside the sandbox.

The get_host() method returns the public URL of the sandbox.

from agentbox import Sandbox

sandbox = Sandbox(api_key="ab_xxxxxxxxxxxxxxxxxxxxxxxxx",
    template="wemmodr8mb2uk3kn7exw",
    timeout=120)

# You need to always pass a port number to get the host
host = sandbox.get_host(3000)
print(f'https://{host}')

The code above will print something like this:

https://3000-xxxxxx-xxxxx.agentbox.cloud

The first leftmost part of the host is the port number we passed to the method.

Connecting to a server running inside the sandbox

Section titled “Connecting to a server running inside the sandbox”

You can start a server inside the sandbox and connect to it using the approach above.

In this example we will start a simple HTTP server that listens on port 3000 and responds with the content of the directory where the server is started.

from agentbox import Sandbox

sandbox = Sandbox(api_key="ab_xxxxxxxxxxxxxxxxxxxxxxxxx",
    template="wemmodr8mb2uk3kn7exw",
    timeout=120)

# Start a simple HTTP server inside the sandbox.
process = sandbox.commands.run("python -m http.server 3000", background=True)
host = sandbox.get_host(3000)
url = f"https://{host}"
print('Server started at:', url)

# Fetch data from the server inside the sandbox.
response = sandbox.commands.run(f"curl {url}")
data = response.stdout
print("Response from server inside sandbox:", data)

# Kill the server process inside the sandbox.
process.kill()

This output will look like this:

Server started at: https://3000-xxx-xxx.agentbox.cloud
Response from server inside sandbox:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Directory listing for /</title>
  </head>
  <body>
    <h1>Directory listing for /</h1>
    <hr>
    <ul>
      <li><a href=".bash_logout">.bash_logout</a></li>
      <li><a href=".bashrc">.bashrc</a></li>
      <li><a href=".profile">.profile</a></li>
    </ul>
    <hr>
  </body>
</html>