Python 3: discovering the magnetic bond contained in a variable

I have a link to a magnet (for example: magnet :? xt = urn: btih: 1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6) contained in a variable as a string and would like the script to open a default program that processes magnetic links to start downloading the torrent (for example if I opened a magnetic link from my file manager).

In order to give clear answers, we will say that we have a link to a magnet in a variable called magnet_link .

+4
source share
3 answers

On Windows, you can use os.startfile :

 os.startfile(magnet_link) 

For Mac / OSX, you can probably use applescript and connect it to osascript , for Linux you can use xdg-open .

+7
source

On mac, if you have an installed application that will handle it, just pass the link to the open command

 open "some url" 

Using something from a subprocess, I would introduce

+1
source

Here is a small piece of code that summarizes the method on all operating systems

  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) 
0
source

Source: https://habr.com/ru/post/1397298/


All Articles