from clore_client import CloreClient
client = CloreClient(api_key="YOUR_API_KEY")
def rent_cheapest_rtx4090(image="cloreai/ubuntu20.04-jupyter", ssh_password="SecurePass123"):
servers = client.list_servers()
# 过滤:可用的 RTX 4090
candidates = [
s for s in servers
if "4090" in s["specs"].get("gpu", "")
and not s["rented"]
]
if not candidates:
raise RuntimeError("未找到可用的 RTX 4090 服务器")
# 按按需 BTC 价格排序
candidates.sort(key=lambda s: s["price"]["on_demand"]["bitcoin"])
best = candidates[0]
price_btc = best["price"]["on_demand"]["bitcoin"]
print(f"租用服务器 {best['id']}:{best['specs']['gpu']} @ {price_btc:.8f} BTC/天")
client.create_order(
server_id=best["id"],
image=image,
order_type="on-demand",
currency="bitcoin",
ports={"22": "tcp"},
ssh_password=ssh_password,
)
print("完成!请查看你的订单以获取 SSH 连接详情。")
return best["id"]
rent_cheapest_rtx4090()
import time
from clore_client import CloreClient
client = CloreClient(api_key="YOUR_API_KEY")
def monitor_orders(poll_interval_seconds=60):
"""轮询订单并打印状态更新。"""
print(f"监控订单(每 {poll_interval_seconds} 秒轮询一次)。按 Ctrl+C 停止。\n")
while True:
orders = client.get_orders(include_completed=False)
active = [o for o in orders if not o.get("expired")]
print(f"--- {len(active)} 个活跃订单 ---")
for order in active:
cluster = order.get("pub_cluster", [])
tcp = order.get("tcp_ports", [])
spend = order.get("spend", 0)
ssh_info = ""
if cluster and tcp:
port = tcp[0].split(":")[1]
ssh_info = f" | SSH: {cluster[0]}:{port}"
print(f" 订单 {order['id']}:服务器 {order['si']}"
f" | 已花费 {spend:.8f} BTC{ssh_info}")
if not active:
print(" 当前没有活跃订单。")
print()
time.sleep(poll_interval_seconds)
monitor_orders()
import time
from clore_client import CloreClient
client = CloreClient(api_key="YOUR_API_KEY")
def auto_rent_on_price_drop(
gpu_model: str = "RTX 4090",
max_price_btc: float = 0.00015,
image: str = "cloreai/ubuntu20.04-jupyter",
ssh_password: str = "SecurePass123",
check_interval_seconds: int = 120,
):
"""
监控市场,当价格低于阈值时自动租用 GPU。
参数:
gpu_model:要搜索的 GPU 名称(不区分大小写)
max_price_btc:可接受的每日最高价格(BTC)
image:要部署的 Docker 镜像
ssh_password:容器的 SSH 密码
check_interval_seconds:检查频率(注意速率限制!)
"""
print(f"正在监控 {gpu_model},目标价格 ≤ {max_price_btc:.8f} BTC/天...")
while True:
servers = client.list_servers()
for server in servers:
gpu = server["specs"].get("gpu", "")
if gpu_model.lower() not in gpu.lower():
continue
if server["rented"]:
continue
price = server["price"]["on_demand"]["bitcoin"]
if price <= max_price_btc:
print(f"🎯 找到匹配项!服务器 {server['id']}:{gpu} @ {price:.8f} BTC/天")
try:
client.create_order(
server_id=server["id"],
image=image,
order_type="on-demand",
currency="bitcoin",
ports={"22": "tcp"},
ssh_password=ssh_password,
required_price=price, # 锁定此价格
)
print(f"✅ 已为服务器 {server['id']} 创建订单!")
return server["id"]
except Exception as e:
print(f"创建订单失败:{e}。将重试……")
print(f"尚未找到匹配项。将在 {check_interval_seconds}s 后再次检查……")
time.sleep(check_interval_seconds)
auto_rent_on_price_drop(gpu_model="4090", max_price_btc=0.00012)
{ "code": 5 }
import time
def safe_api_call(fn, *args, delay=1.1, **kwargs):
"""带速率限制安全保护的 API 调用包装。"""
result = fn(*args, **kwargs)
time.sleep(delay)
return result