#! /usr/bin/env python3"""Interfaces for launching and remotely controlling Web browsers."""# Maintained by Georg Brandl.import osimport shleximport shutilimport sysimport subprocessimport threading__all__ = ["Error", "open", "open_new", "open_new_tab", "get", "register"]class Error(Exception):pass_lock = threading.RLock()_browsers = {} # Dictionary of available browser controllers_tryorder = None # Preference order of available browsers_os_preferred_browser = None # The preferred browserdef register(name, klass, instance=None, *, preferred=False):"""Register a browser connector."""with _lock:if _tryorder is None:register_standard_browsers()_browsers[name.lower()] = [klass, instance]# Preferred browsers go to the front of the list.# Need to match to the default browser returned by xdg-settings, which# may be of the form e.g. "firefox.desktop".if preferred or (_os_preferred_browser and name in _os_preferred_browser):_tryorder.insert(0, name)else:_tryorder.append(name)def get(using=None):"""Return a browser launcher instance appropriate for the environment."""if _tryorder is None:with _lock:if _tryorder is None:register_standard_browsers()if using is not None:alternatives = [using]else:alternatives = _tryorderfor browser in alternatives:if '%s' in browser:# User gave us a command line, split it into name and argsbrowser = shlex.split(browser)if browser[-1] == '&':return BackgroundBrowser(browser[:-1])else:return GenericBrowser(browser)else:# User gave us a browser name or path.try:command = _browsers[browser.lower()]except KeyError:command = _synthesize(browser)if command[1] is not None:return command[1]elif command[0] is not None:return command[0]()raise Error("could not locate runnable browser")# Please note: the following definition hides a builtin function.# It is recommended one does "import webbrowser" and uses webbrowser.open(url)# instead of "from webbrowser import *".def open(url, new=0, autoraise=True):if _tryorder is None:with _lock:if _tryorder is None:register_standard_browsers()for name in _tryorder:browser = get(name)if browser.open(url, new, autoraise):return Truereturn Falsedef open_new(url):return open(url, 1)def open_new_tab(url):return open(url, 2)def _synthesize(browser, *, preferred=False):"""Attempt to synthesize a controller base on existing controllers.This is useful to create a controller when a user specifies a path toan entry in the BROWSER environment variable -- we can copy a generalcontroller to operate using a specific installation of the desiredbrowser in this way.If we can't create a controller in this way, or if there is noexecutable for the requested browser, return [None, None]."""cmd = browser.split()[0]if not shutil.which(cmd):return [None, None]name = os.path.basename(cmd)try:command = _browsers[name.lower()]except KeyError:return [None, None]# now attempt to clone to fit the new name:controller = command[1]if controller and name.lower() == controller.basename:import copycontroller = copy.copy(controller)controller.name = browsercontroller.basename = os.path.basename(browser)register(browser, None, instance=controller, preferred=preferred)return [None, controller]return [None, None]# General parent classesclass BaseBrowser(object):"""Parent class for all browsers. Do not use directly."""args = ['%s']def __init__(self, name=""):self.name = nameself.basename = namedef open(self, url, new=0, autoraise=True):raise NotImplementedErrordef open_new(self, url):return self.open(url, 1)def open_new_tab(self, url):return self.open(url, 2)class GenericBrowser(BaseBrowser):"""Class for all browsers started with a commandand without remote functionality."""def __init__(self, name):if isinstance(name, str):self.name = nameself.args = ["%s"]else:# name should be a list with argumentsself.name = name[0]self.args = name[1:]self.basename = os.path.basename(self.name)def open(self, url, new=0, autoraise=True):sys.audit("webbrowser.open", url)cmdline = [self.name] + [arg.replace("%s", url)for arg in self.args]try:if sys.platform[:3] == 'win':p = subprocess.Popen(cmdline)else:p = subprocess.Popen(cmdline, close_fds=True)return not p.wait()except OSError:return Falseclass BackgroundBrowser(GenericBrowser):"""Class for all browsers which are to be started in thebackground."""def open(self, url, new=0, autoraise=True):cmdline = [self.name] + [arg.replace("%s", url)for arg in self.args]sys.audit("webbrowser.open", url)try:if sys.platform[:3] == 'win':p = subprocess.Popen(cmdline)else:p = subprocess.Popen(cmdline, close_fds=True,start_new_session=True)return (p.poll() is None)except OSError:return Falseclass UnixBrowser(BaseBrowser):"""Parent class for all Unix browsers with remote functionality."""raise_opts = Nonebackground = Falseredirect_stdout = True# In remote_args, %s will be replaced with the requested URL. %action will# be replaced depending on the value of 'new' passed to open.# remote_action is used for new=0 (open). If newwin is not None, it is# used for new=1 (open_new). If newtab is not None, it is used for# new=3 (open_new_tab). After both substitutions are made, any empty# strings in the transformed remote_args list will be removed.remote_args = ['%action', '%s']remote_action = Noneremote_action_newwin = Noneremote_action_newtab = Nonedef _invoke(self, args, remote, autoraise, url=None):raise_opt = []if remote and self.raise_opts:# use autoraise argument only for remote invocationautoraise = int(autoraise)opt = self.raise_opts[autoraise]if opt: raise_opt = [opt]cmdline = [self.name] + raise_opt + argsif remote or self.background:inout = subprocess.DEVNULLelse:# for TTY browsers, we need stdin/outinout = Nonep = subprocess.Popen(cmdline, close_fds=True, stdin=inout,stdout=(self.redirect_stdout and inout or None),stderr=inout, start_new_session=True)if remote:# wait at most five seconds. If the subprocess is not finished, the# remote invocation has (hopefully) started a new instance.try:rc = p.wait(5)# if remote call failed, open() will try direct invocationreturn not rcexcept subprocess.TimeoutExpired:return Trueelif self.background:if p.poll() is None:return Trueelse:return Falseelse:return not p.wait()def open(self, url, new=0, autoraise=True):sys.audit("webbrowser.open", url)if new == 0:action = self.remote_actionelif new == 1:action = self.remote_action_newwinelif new == 2:if self.remote_action_newtab is None:action = self.remote_action_newwinelse:action = self.remote_action_newtabelse:raise Error("Bad 'new' parameter to open(); " +"expected 0, 1, or 2, got %s" % new)args = [arg.replace("%s", url).replace("%action", action)for arg in self.remote_args]args = [arg for arg in args if arg]success = self._invoke(args, True, autoraise, url)if not success:# remote invocation failed, try straight wayargs = [arg.replace("%s", url) for arg in self.args]return self._invoke(args, False, False)else:return Trueclass Mozilla(UnixBrowser):"""Launcher class for Mozilla browsers."""remote_args = ['%action', '%s']remote_action = ""remote_action_newwin = "-new-window"remote_action_newtab = "-new-tab"background = Trueclass Netscape(UnixBrowser):"""Launcher class for Netscape browser."""raise_opts = ["-noraise", "-raise"]remote_args = ['-remote', 'openURL(%s%action)']remote_action = ""remote_action_newwin = ",new-window"remote_action_newtab = ",new-tab"background = Trueclass Galeon(UnixBrowser):"""Launcher class for Galeon/Epiphany browsers."""raise_opts = ["-noraise", ""]remote_args = ['%action', '%s']remote_action = "-n"remote_action_newwin = "-w"background = Trueclass Chrome(UnixBrowser):"Launcher class for Google Chrome browser."remote_args = ['%action', '%s']remote_action = ""remote_action_newwin = "--new-window"remote_action_newtab = ""background = TrueChromium = Chromeclass Opera(UnixBrowser):"Launcher class for Opera browser."remote_args = ['%action', '%s']remote_action = ""remote_action_newwin = "--new-window"remote_action_newtab = ""background = Trueclass Elinks(UnixBrowser):"Launcher class for Elinks browsers."remote_args = ['-remote', 'openURL(%s%action)']remote_action = ""remote_action_newwin = ",new-window"remote_action_newtab = ",new-tab"background = False# elinks doesn't like its stdout to be redirected -# it uses redirected stdout as a signal to do -dumpredirect_stdout = Falseclass Konqueror(BaseBrowser):"""Controller for the KDE File Manager (kfm, or Konqueror).See the output of ``kfmclient --commands``for more information on the Konqueror remote-control interface."""def open(self, url, new=0, autoraise=True):sys.audit("webbrowser.open", url)# XXX Currently I know no way to prevent KFM from opening a new win.if new == 2:action = "newTab"else:action = "openURL"devnull = subprocess.DEVNULLtry:p = subprocess.Popen(["kfmclient", action, url],close_fds=True, stdin=devnull,stdout=devnull, stderr=devnull)except OSError:# fall through to next variantpasselse:p.wait()# kfmclient's return code unfortunately has no meaning as it seemsreturn Truetry:p = subprocess.Popen(["konqueror", "--silent", url],close_fds=True, stdin=devnull,stdout=devnull, stderr=devnull,start_new_session=True)except OSError:# fall through to next variantpasselse:if p.poll() is None:# Should be running now.return Truetry:p = subprocess.Popen(["kfm", "-d", url],close_fds=True, stdin=devnull,stdout=devnull, stderr=devnull,start_new_session=True)except OSError:return Falseelse:return (p.poll() is None)class Grail(BaseBrowser):# There should be a way to maintain a connection to Grail, but the# Grail remote control protocol doesn't really allow that at this# point. It probably never will!def _find_grail_rc(self):import globimport pwdimport socketimport tempfiletempdir = os.path.join(tempfile.gettempdir(),".grail-unix")user = pwd.getpwuid(os.getuid())[0]filename = os.path.join(tempdir, user + "-*")maybes = glob.glob(filename)if not maybes:return Nones = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)for fn in maybes:# need to PING each one until we find one that's livetry:s.connect(fn)except OSError:# no good; attempt to clean it out, but don't fail:try:os.unlink(fn)except OSError:passelse:return sdef _remote(self, action):s = self._find_grail_rc()if not s:return 0s.send(action)s.close()return 1def open(self, url, new=0, autoraise=True):sys.audit("webbrowser.open", url)if new:ok = self._remote("LOADNEW " + url)else:ok = self._remote("LOAD " + url)return ok## Platform support for Unix## These are the right tests because all these Unix browsers require either# a console terminal or an X display to run.def register_X_browsers():# use xdg-open if aroundif shutil.which("xdg-open"):register("xdg-open", None, BackgroundBrowser("xdg-open"))# The default GNOME3 browserif "GNOME_DESKTOP_SESSION_ID" in os.environ and shutil.which("gvfs-open"):register("gvfs-open", None, BackgroundBrowser("gvfs-open"))# The default GNOME browserif "GNOME_DESKTOP_SESSION_ID" in os.environ and shutil.which("gnome-open"):register("gnome-open", None, BackgroundBrowser("gnome-open"))# The default KDE browserif "KDE_FULL_SESSION" in os.environ and shutil.which("kfmclient"):register("kfmclient", Konqueror, Konqueror("kfmclient"))if shutil.which("x-www-browser"):register("x-www-browser", None, BackgroundBrowser("x-www-browser"))# The Mozilla browsersfor browser in ("firefox", "iceweasel", "iceape", "seamonkey"):if shutil.which(browser):register(browser, None, Mozilla(browser))# The Netscape and old Mozilla browsersfor browser in ("mozilla-firefox","mozilla-firebird", "firebird","mozilla", "netscape"):if shutil.which(browser):register(browser, None, Netscape(browser))# Konqueror/kfm, the KDE browser.if shutil.which("kfm"):register("kfm", Konqueror, Konqueror("kfm"))elif shutil.which("konqueror"):register("konqueror", Konqueror, Konqueror("konqueror"))# Gnome's Galeon and Epiphanyfor browser in ("galeon", "epiphany"):if shutil.which(browser):register(browser, None, Galeon(browser))# Skipstone, another Gtk/Mozilla based browserif shutil.which("skipstone"):register("skipstone", None, BackgroundBrowser("skipstone"))# Google Chrome/Chromium browsersfor browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):if shutil.which(browser):register(browser, None, Chrome(browser))# Opera, quite popularif shutil.which("opera"):register("opera", None, Opera("opera"))# Next, Mosaic -- old but still in use.if shutil.which("mosaic"):register("mosaic", None, BackgroundBrowser("mosaic"))# Grail, the Python browser. Does anybody still use it?if shutil.which("grail"):register("grail", Grail, None)def register_standard_browsers():global _tryorder_tryorder = []if sys.platform == 'darwin':register("MacOSX", None, MacOSXOSAScript('default'))register("chrome", None, MacOSXOSAScript('chrome'))register("firefox", None, MacOSXOSAScript('firefox'))register("safari", None, MacOSXOSAScript('safari'))# OS X can use below Unix support (but we prefer using the OS X# specific stuff)if sys.platform[:3] == "win":# First try to use the default Windows browserregister("windows-default", WindowsDefault)# Detect some common Windows browsers, fallback to IEiexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),"Internet Explorer\\IEXPLORE.EXE")for browser in ("firefox", "firebird", "seamonkey", "mozilla","netscape", "opera", iexplore):if shutil.which(browser):register(browser, None, BackgroundBrowser(browser))else:# Prefer X browsers if presentif os.environ.get("DISPLAY"):try:cmd = "xdg-settings get default-web-browser".split()raw_result = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)result = raw_result.decode().strip()except (FileNotFoundError, subprocess.CalledProcessError):passelse:global _os_preferred_browser_os_preferred_browser = resultregister_X_browsers()# Also try console browsersif os.environ.get("TERM"):if shutil.which("www-browser"):register("www-browser", None, GenericBrowser("www-browser"))# The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/>if shutil.which("links"):register("links", None, GenericBrowser("links"))if shutil.which("elinks"):register("elinks", None, Elinks("elinks"))# The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/>if shutil.which("lynx"):register("lynx", None, GenericBrowser("lynx"))# The w3m browser <http://w3m.sourceforge.net/>if shutil.which("w3m"):register("w3m", None, GenericBrowser("w3m"))# OK, now that we know what the default preference orders for each# platform are, allow user to override them with the BROWSER variable.if "BROWSER" in os.environ:userchoices = os.environ["BROWSER"].split(os.pathsep)userchoices.reverse()# Treat choices in same way as if passed into get() but do register# and prepend to _tryorderfor cmdline in userchoices:if cmdline != '':cmd = _synthesize(cmdline, preferred=True)if cmd[1] is None:register(cmdline, None, GenericBrowser(cmdline), preferred=True)# what to do if _tryorder is now empty?## Platform support for Windows#if sys.platform[:3] == "win":class WindowsDefault(BaseBrowser):def open(self, url, new=0, autoraise=True):sys.audit("webbrowser.open", url)try:os.startfile(url)except OSError:# [Error 22] No application is associated with the specified# file for this operation: '<URL>'return Falseelse:return True## Platform support for MacOS#if sys.platform == 'darwin':# Adapted from patch submitted to SourceForge by Steven J. Burrclass MacOSX(BaseBrowser):"""Launcher class for Aqua browsers on Mac OS XOptionally specify a browser name on instantiation. Note that thiswill not work for Aqua browsers if the user has moved the applicationpackage after installation.If no browser is specified, the default browser, as specified in theInternet System Preferences panel, will be used."""def __init__(self, name):self.name = namedef open(self, url, new=0, autoraise=True):sys.audit("webbrowser.open", url)assert "'" not in url# hack for local urlsif not ':' in url:url = 'file:'+url# new must be 0 or 1new = int(bool(new))if self.name == "default":# User called open, open_new or get without a browser parameterscript = 'open location "%s"' % url.replace('"', '%22') # opens in default browserelse:# User called get and chose a browserif self.name == "OmniWeb":toWindow = ""else:# Include toWindow parameter of OpenURL command for browsers# that support it. 0 == new window; -1 == existingtoWindow = "toWindow %d" % (new - 1)cmd = 'OpenURL "%s"' % url.replace('"', '%22')script = '''tell application "%s"activate%s %send tell''' % (self.name, cmd, toWindow)# Open pipe to AppleScript through osascript commandosapipe = os.popen("osascript", "w")if osapipe is None:return False# Write script to osascript's stdinosapipe.write(script)rc = osapipe.close()return not rcclass MacOSXOSAScript(BaseBrowser):def __init__(self, name):self._name = namedef open(self, url, new=0, autoraise=True):if self._name == 'default':script = 'open location "%s"' % url.replace('"', '%22') # opens in default browserelse:script = '''tell application "%s"activateopen location "%s"end'''%(self._name, url.replace('"', '%22'))osapipe = os.popen("osascript", "w")if osapipe is None:return Falseosapipe.write(script)rc = osapipe.close()return not rcdef main():import getoptusage = """Usage: %s [-n | -t] url-n: open new window-t: open new tab""" % sys.argv[0]try:opts, args = getopt.getopt(sys.argv[1:], 'ntd')except getopt.error as msg:print(msg, file=sys.stderr)print(usage, file=sys.stderr)sys.exit(1)new_win = 0for o, a in opts:if o == '-n': new_win = 1elif o == '-t': new_win = 2if len(args) != 1:print(usage, file=sys.stderr)sys.exit(1)url = args[0]open(url, new_win)print("\a")if __name__ == "__main__":main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。