Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:打开变量中包含的磁铁链接_Python_Variables_Python 3.x_Bittorrent_Utorrent - Fatal编程技术网

Python 3:打开变量中包含的磁铁链接

Python 3:打开变量中包含的磁铁链接,python,variables,python-3.x,bittorrent,utorrent,Python,Variables,Python 3.x,Bittorrent,Utorrent,我有一个磁铁链接(例如:magnet:?xt=urn:btih:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6)作为字符串包含在变量中,希望脚本打开处理磁铁链接的默认程序,以便它开始下载torrent(就像我从文件管理器中打开磁铁链接一样) 为了明确答案,我们会说,在Windows上,在名为magnet\u-link的变量中有磁铁链接,您可以使用: 对于Mac/OSX,您可能可以使用applescript并将其传输到osascript,对于Linux,您可能可以

我有一个磁铁链接(例如:magnet:?xt=urn:btih:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6)作为字符串包含在变量中,希望脚本打开处理磁铁链接的默认程序,以便它开始下载torrent(就像我从文件管理器中打开磁铁链接一样)

为了明确答案,我们会说,在Windows上,在名为
magnet\u-link

的变量中有磁铁链接,您可以使用:


对于Mac/OSX,您可能可以使用applescript并将其传输到
osascript
,对于Linux,您可能可以在Mac上使用
xdg open

,如果您安装了一个应用程序来处理它,只需将链接传递到open命令即可

open "some url"
使用子流程中的一些东西,我可以想象

这里有一个小代码片段,总结了所有操作系统上的方法
我自己也在Windows上,但如果您能给我Mac和Linux的命令,然后我可以使用os.name或其他等效工具来查找操作系统,以便使用正确的命令。我得到以下错误:
WindowsError:[error-2147217406]Windows错误0x%X:'magent:?xt=urn:btich:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6'
哪个操作系统?你确定你有一个与magnet协议相关联的应用程序吗?为了完整性:在Linux上,你可能可以用它调用
xdg open
命令。@EdenCrow:尝试
magnet:…
而不是
magent:…
它也应该是
magnet:?xt=urn:btih
而不是
btich
。再想一想,open命令也允许您指定应用程序。
open "some url"
  import sys , subprocess
  def open_magnet(magnet):
        """Open magnet according to os."""
        if sys.platform.startswith('linux'):
            subprocess.Popen(['xdg-open', magnet],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        elif sys.platform.startswith('win32'):
            os.startfile(magnet)
        elif sys.platform.startswith('cygwin'):
            os.startfile(magnet)
        elif sys.platform.startswith('darwin'):
            subprocess.Popen(['open', magnet],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        else:
            subprocess.Popen(['xdg-open', magnet],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)