# Public URL
<subtitle>Get the public URL of the sandbox and connect to the service running in the sandbox. </subtitle>

Each sandbox is accessible via a public URL, which allows you to expose and connect to the services running within it. This page describes how to obtain the URL, connect to a server within the sandbox, and customize the `Host` header received by the service.

To control outbound Internet access from the sandbox, see [Network Access](/docs/agent-sandbox/network/internet-access.md). To require authentication on public URLs, see [Restrict Public Access](/docs/agent-sandbox/network/restrict-public-access.md).

## Sandbox public URL

Each sandbox has a public URL that can be used to access services running within the sandbox.

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()

# Always need to pass in the port number when getting the host
host = sandbox.get_host(3000)
print(f'https://{host}')
```

The above code will print something similar to the following:

```bash
https://3000-<sandbox-id>.cn-wlcb.sandbox.ucloudai.com
```

The first left-most part of host is the port number we passed to the method.

## Connect to a server running within the sandbox

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

In this example, we will start a simple HTTP server that listens on port 3000 and returns the contents of the directory where the server was started.

```python
import requests
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()

# Start a simple HTTP server in 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)

# Get data from the server in the sandbox
response = requests.get(url)
data = response.text
print('Response from server inside sandbox:', data)

# Kill the server process in the sandbox
process.kill()
```

The output will look like this:

```bash
Server started at: https://3000-<sandbox-id>.cn-wlcb.sandbox.ucloudai.com
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>
```

## Mask request Host header

You can use the `mask_request_host` option to customize the `Host` header sent to services running inside the sandbox. This is useful when your application requires a specific host format.

```python
from ucloud_sandbox import Sandbox

# Create a sandbox with custom host mask
sandbox = Sandbox.create(
    network={
        "mask_request_host": "localhost:${PORT}"
    }
)

# ${PORT} variable will be replaced with the actual port number
# The Host header of requests sent to the sandbox will be set to, for example: localhost:8080
```

The `${PORT}` variable in the mask is automatically replaced with the actual port number of the requested service.
