Spaces:
Running
Running
Commit ·
861735d
1
Parent(s): af7a9e0
debug: real /api/sftp-test with socket+paramiko timeouts
Browse files- backend/api.py +52 -8
backend/api.py
CHANGED
|
@@ -2,6 +2,8 @@ from __future__ import annotations
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import concurrent.futures
|
| 6 |
|
| 7 |
from pathlib import Path
|
|
@@ -225,14 +227,56 @@ def jobs_status(job_id: str, req: StatusReq, x_worker_token: Optional[str] = Hea
|
|
| 225 |
job.message = req.message
|
| 226 |
return {"ok": True, "job_id": job_id, "status": job.status}
|
| 227 |
|
| 228 |
-
|
| 229 |
@app.get("/api/sftp-test")
|
| 230 |
def api_sftp_test():
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
try:
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import socket
|
| 6 |
+
import paramiko
|
| 7 |
import concurrent.futures
|
| 8 |
|
| 9 |
from pathlib import Path
|
|
|
|
| 227 |
job.message = req.message
|
| 228 |
return {"ok": True, "job_id": job_id, "status": job.status}
|
| 229 |
|
|
|
|
| 230 |
@app.get("/api/sftp-test")
|
| 231 |
def api_sftp_test():
|
| 232 |
+
host = (os.getenv("SFTP_HOST") or "").strip()
|
| 233 |
+
port = int((os.getenv("SFTP_PORT") or "2222").strip())
|
| 234 |
+
user = (os.getenv("SFTP_USER") or "").strip()
|
| 235 |
+
pw = (os.getenv("SFTP_PASS") or "").strip()
|
| 236 |
+
root = (os.getenv("SFTP_ROOT") or ".").strip() or "."
|
| 237 |
+
timeout = float((os.getenv("SFTP_TIMEOUT_SECONDS") or "8").strip())
|
| 238 |
+
|
| 239 |
+
if not host or not user or not pw:
|
| 240 |
+
raise HTTPException(status_code=500, detail="Missing SFTP secrets: SFTP_HOST/SFTP_USER/SFTP_PASS")
|
| 241 |
+
|
| 242 |
+
t0 = time.time()
|
| 243 |
+
# HARD socket timeout
|
| 244 |
+
sock = socket.create_connection((host, port), timeout=timeout)
|
| 245 |
+
t_sock = time.time()
|
| 246 |
+
|
| 247 |
+
tr = paramiko.Transport(sock)
|
| 248 |
+
tr.banner_timeout = timeout
|
| 249 |
+
tr.auth_timeout = timeout
|
| 250 |
+
|
| 251 |
+
tr.connect(username=user, password=pw)
|
| 252 |
+
t_auth = time.time()
|
| 253 |
+
|
| 254 |
+
sftp = paramiko.SFTPClient.from_transport(tr)
|
| 255 |
+
|
| 256 |
+
out = {
|
| 257 |
+
"ok": False,
|
| 258 |
+
"host": host,
|
| 259 |
+
"port": port,
|
| 260 |
+
"user": user,
|
| 261 |
+
"root": root,
|
| 262 |
+
"timeout": timeout,
|
| 263 |
+
"t_sock_ms": int((t_sock - t0) * 1000),
|
| 264 |
+
"t_auth_ms": int((t_auth - t_sock) * 1000),
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
try:
|
| 268 |
+
sftp.chdir(root)
|
| 269 |
+
out["listdir_root"] = sftp.listdir(".")[:50]
|
| 270 |
+
try:
|
| 271 |
+
st = sftp.stat("pdfs/demo_test_003.pdf")
|
| 272 |
+
out["stat_demo_test_003"] = {"size": st.st_size}
|
| 273 |
+
except Exception as e:
|
| 274 |
+
out["stat_demo_test_003_error"] = str(e)
|
| 275 |
+
out["ok"] = True
|
| 276 |
+
return JSONResponse(out)
|
| 277 |
+
finally:
|
| 278 |
+
try:
|
| 279 |
+
sftp.close()
|
| 280 |
+
finally:
|
| 281 |
+
tr.close()
|
| 282 |
+
|