Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import os
- def get_protocol_info(protocol):
- print(f"--- 正在查询协议: {protocol} ---")
- try:
- # 1. 查询该协议关联的 .desktop 文件名
- # 相当于执行: xdg-mime query default x-scheme-handler/r34
- mime_type = f"x-scheme-handler/{protocol}"
- cmd = ["xdg-mime", "query", "default", mime_type]
- desktop_file = subprocess.check_output(cmd).decode('utf-8').strip()
- if not desktop_file:
- print(f"错误:系统中未找到 {protocol} 协议的关联程序。")
- return
- print(f"关联的配置文件: {desktop_file}")
- # 2. 搜索该 .desktop 文件的实际路径
- # 通常在 /usr/share/applications 或 ~/.local/share/applications
- search_paths = [
- os.path.expanduser("~/.local/share/applications"),
- "/usr/share/applications",
- "/usr/local/share/applications"
- ]
- found_path = None
- for path in search_paths:
- full_path = os.path.join(path, desktop_file)
- if os.path.exists(full_path):
- found_path = full_path
- break
- if found_path:
- print(f"文件完整路径: {found_path}")
- print("\n--- 配置文件内容如下 ---")
- with open(found_path, 'r') as f:
- print(f.read())
- else:
- print("错误:找到了关联名,但在标准目录中未找到对应的 .desktop 文件。")
- except FileNotFoundError:
- print("错误:系统中未安装 xdg-utils,请先安装。")
- except Exception as e:
- print(f"发生错误: {e}")
- if __name__ == "__main__":
- get_protocol_info("r34")
Advertisement
Add Comment
Please, Sign In to add comment