Guest User

Untitled

a guest
Dec 24th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import subprocess
  2. import os
  3.  
  4. def get_protocol_info(protocol):
  5. print(f"--- 正在查询协议: {protocol} ---")
  6.  
  7. try:
  8. # 1. 查询该协议关联的 .desktop 文件名
  9. # 相当于执行: xdg-mime query default x-scheme-handler/r34
  10. mime_type = f"x-scheme-handler/{protocol}"
  11. cmd = ["xdg-mime", "query", "default", mime_type]
  12. desktop_file = subprocess.check_output(cmd).decode('utf-8').strip()
  13.  
  14. if not desktop_file:
  15. print(f"错误:系统中未找到 {protocol} 协议的关联程序。")
  16. return
  17.  
  18. print(f"关联的配置文件: {desktop_file}")
  19.  
  20. # 2. 搜索该 .desktop 文件的实际路径
  21. # 通常在 /usr/share/applications 或 ~/.local/share/applications
  22. search_paths = [
  23. os.path.expanduser("~/.local/share/applications"),
  24. "/usr/share/applications",
  25. "/usr/local/share/applications"
  26. ]
  27.  
  28. found_path = None
  29. for path in search_paths:
  30. full_path = os.path.join(path, desktop_file)
  31. if os.path.exists(full_path):
  32. found_path = full_path
  33. break
  34.  
  35. if found_path:
  36. print(f"文件完整路径: {found_path}")
  37. print("\n--- 配置文件内容如下 ---")
  38. with open(found_path, 'r') as f:
  39. print(f.read())
  40. else:
  41. print("错误:找到了关联名,但在标准目录中未找到对应的 .desktop 文件。")
  42.  
  43. except FileNotFoundError:
  44. print("错误:系统中未安装 xdg-utils,请先安装。")
  45. except Exception as e:
  46. print(f"发生错误: {e}")
  47.  
  48. if __name__ == "__main__":
  49. get_protocol_info("r34")
Advertisement
Add Comment
Please, Sign In to add comment