沙箱公共 URL
每个沙箱都能提供公共 URL,用于访问沙箱内运行的服务。import { Sandbox } from 'ppio-sandbox/code-interpreter'
const sandbox = await Sandbox.create()
// 您需要传入一个端口号来获取公共 URL。
const host = sandbox.getHost(3000)
console.log(`https://${host}`)
// 输出示例:
// https://3000-icl2ke7w1grxep2o771m1-abf219fd.sandbox.ppio.cn
await sandbox.kill()
from ppio_sandbox.code_interpreter import Sandbox
sandbox = Sandbox.create()
# 您需要传入一个端口号来获取公共 URL。
host = sandbox.get_host(3000)
print(f'https://{host}')
# 输出示例:
# https://3000-ik56flymp4xczmvmt97lk-abf219fd.sandbox.ppio.cn
sandbox.kill()
访问沙箱内运行的服务
您可以在沙箱内启动服务并通过一个公共 URL 访问它。 在这个示例中,我们将启动一个简单的 HTTP 服务器,监听端口 3000,并返回启动服务器的目录信息。import { Sandbox } from 'ppio-sandbox/code-interpreter'
const sandbox = await Sandbox.create()
// 启动一个简单的 HTTP 服务器。
const commandHandle = await sandbox.commands.run('python -m http.server 3000', { background: true })
const host = sandbox.getHost(3000)
const url = `https://${host}`
console.log('Server started at:', url)
// 从服务器获取数据。
const response = await fetch(url);
const data = await response.text();
console.log('Response from server inside sandbox:', data);
// 终止服务器进程。
await commandHandle.kill()
await sandbox.kill()
from ppio_sandbox.code_interpreter import Sandbox
sandbox = Sandbox.create()
# 启动一个简单的 HTTP 服务器。
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)
# 从服务器获取数据。
response = sandbox.commands.run(f"curl {url}")
data = response.stdout
print("Response from server inside sandbox:", data)
# 终止服务器进程。
process.kill()
sandbox.kill()