jietu.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import paramiko
  2. from scp import SCPClient
  3. import os
  4. import time
  5. # ===================== 配置 =====================
  6. JUMP_HOST = "183.252.196.135"
  7. JUMP_PORT = 22
  8. JUMP_USER = "root"
  9. # 如果你有密码就填密码,有密钥就留空
  10. JUMP_PASSWORD = "9Mp$ctw6k~(W" # 有密码就写在这里
  11. JUMP_KEY_PATH = None # 有密钥文件就填路径
  12. # 远程 wav 目录
  13. REMOTE_WAV_DIR = "/opt/upload-service/static-6009/RTMP_pict/wav"
  14. # 本地保存目录
  15. LOCAL_DIR = r"/Users/alien/Desktop/Digital_Human/Image_Analysis/wav/wav"
  16. # 拉取间隔(秒)
  17. INTERVAL = 1
  18. # =================================================
  19. # 确保本地文件夹存在
  20. os.makedirs(LOCAL_DIR, exist_ok=True)
  21. def create_ssh():
  22. ssh = paramiko.SSHClient()
  23. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  24. try:
  25. if JUMP_PASSWORD:
  26. ssh.connect(
  27. JUMP_HOST,
  28. port=JUMP_PORT,
  29. username=JUMP_USER,
  30. password=JUMP_PASSWORD,
  31. timeout=10
  32. )
  33. else:
  34. ssh.connect(
  35. JUMP_HOST,
  36. port=JUMP_PORT,
  37. username=JUMP_USER,
  38. key_filename=JUMP_KEY_PATH,
  39. timeout=10
  40. )
  41. return ssh
  42. except Exception as e:
  43. return None
  44. def list_remote_files(ssh):
  45. try:
  46. stdin, stdout, stderr = ssh.exec_command(f"ls -1 {REMOTE_WAV_DIR}")
  47. return [line.strip() for line in stdout if line.strip()]
  48. except:
  49. return []
  50. def pull_new_files():
  51. ssh = create_ssh()
  52. if not ssh:
  53. return
  54. try:
  55. remote_files = list_remote_files(ssh)
  56. local_files = set(os.listdir(LOCAL_DIR))
  57. new_files = [f for f in remote_files if f not in local_files and f.endswith(".wav")]
  58. if new_files:
  59. with SCPClient(ssh.get_transport()) as scp:
  60. for f in new_files:
  61. remote_path = f"{REMOTE_WAV_DIR}/{f}"
  62. local_path = os.path.join(LOCAL_DIR, f)
  63. scp.get(remote_path, local_path)
  64. print(f"✅ 已拉取: {f}")
  65. except Exception as e:
  66. pass
  67. finally:
  68. ssh.close()
  69. if __name__ == "__main__":
  70. print("=" * 60)
  71. print("🚀 本地实时拉取 wav 音频(纯Python版)")
  72. print(f"📂 本地: {LOCAL_DIR}")
  73. print("=" * 60)
  74. while True:
  75. pull_new_files()
  76. time.sleep(INTERVAL)