Merge pull request #156 from zeronet-conservancy/fix_open_browser

consolidate & fix open browser behaviour
This commit is contained in:
caryoscelus 2022-09-07 14:32:56 +00:00 committed by GitHub
commit c5876b1a0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 30 deletions

View file

@ -13,6 +13,7 @@ from Config import config
from Debug import Debug
import importlib
from util import helper
# Skip websocket handler if not necessary
class UiWSGIHandler(WebSocketHandler):
@ -144,22 +145,7 @@ class UiServer:
self.log.info("Web interface: http://%s:%s/" % (config.ui_ip, config.ui_port))
self.log.info("--------------------------------------")
if config.open_browser and config.open_browser != "False":
logging.info("Opening browser: %s...", config.open_browser)
import webbrowser
ui_ip = config.ui_ip if config.ui_ip != "*" else "127.0.0.1"
url = f'http://{ui_ip}:{config.ui_port}/{config.homepage}'
try:
if config.open_browser == "default_browser":
browser = webbrowser.get()
else:
browser = webbrowser.get(config.open_browser)
gevent.spawn_later(0.3, browser.open, url, new=2)
except webbrowser.Error as err:
import subprocess
subprocess.Popen([config.open_browser, url])
except Exception as err:
print("Error starting browser: %s" % err)
helper.openBrowser(config.open_browser)
self.server = WSGIServer((self.ip, self.port), handler, handler_class=UiWSGIHandler, log=self.log)
self.server.sockets = {}

View file

@ -69,20 +69,8 @@ if config.action == "main":
lock = helper.openLocked("%s/lock.pid" % config.data_dir, "w")
lock.write("%s" % os.getpid())
except BlockingIOError as err:
startupError("Can't open lock file, your ZeroNet client is probably already running, exiting... (%s)" % err)
if config.open_browser and config.open_browser != "False":
print("Opening browser: %s...", config.open_browser)
import webbrowser
try:
if config.open_browser == "default_browser":
browser = webbrowser.get()
else:
browser = webbrowser.get(config.open_browser)
browser.open("http://%s:%s/%s" % (
config.ui_ip if config.ui_ip != "*" else "127.0.0.1", config.ui_port, config.homepage
), new=2)
except Exception as err:
startupError("Error starting browser: %s" % err)
startupError(f"Can't open lock file, your 0net client is probably already running, exiting... ({err})")
helper.openBrowser(config.open_browser)
sys.exit()
config.initLogging()

View file

@ -354,3 +354,14 @@ def encodeResponse(func): # Encode returned data from utf8 to bytes
yield back.encode()
return wrapper
def openBrowser(agent):
if agent and agent != "False":
print(f"Opening browser: {agent}...")
ui_ip = config.ui_ip if config.ui_ip != "*" else "127.0.0.1"
url = f'http://{ui_ip}:{config.ui_port}/{config.homepage}'
try:
import subprocess
subprocess.Popen([config.open_browser, url])
except Exception as err:
print(f"Error starting browser: {err}")