-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
65 lines (53 loc) · 2.38 KB
/
browser.py
File metadata and controls
65 lines (53 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
import shutil
import subprocess
import webbrowser
import json
import urllib.request
import urllib.error
def open_browser(url: str) -> None:
"""Open a URL in the system's default browser, detecting OS and distro.
When running inside Docker on Mac or Windows, browser requests are
forwarded to the host machine via browser_helper_server.py on port 9876.
"""
host_os = os.environ.get("HOST_OS", "").lower()
# ── Inside Docker → Mac or Windows host: forward to helper ───────────────
if host_os in ("mac", "windows"):
host = os.environ.get("DOCKER_HOST_IP", "host.docker.internal")
try:
payload = json.dumps({"url": url}).encode()
req = urllib.request.Request(
f"http://{host}:9876",
data=payload,
headers={"Content-Type": "application/json"},
method="POST",
)
urllib.request.urlopen(req, timeout=5)
return
except urllib.error.URLError:
print(f"[browser] Host helper unreachable — open manually: {url}")
return
# ── Windows native ────────────────────────────────────────────────────────
if sys.platform == "win32":
os.startfile(url)
return
# ── macOS native ──────────────────────────────────────────────────────────
if sys.platform == "darwin":
subprocess.Popen(["open", url])
return
# ── Linux (native or Docker with HOST_OS=linux) ───────────────────────────
for cmd in ["xdg-open", "gio", "gnome-open", "kde-open", "x-www-browser"]:
if shutil.which(cmd):
subprocess.Popen([cmd, url])
return
desktop = os.environ.get("XDG_CURRENT_DESKTOP", "").lower()
if "gnome" in desktop and shutil.which("gnome-open"):
subprocess.Popen(["gnome-open", url])
return
if "kde" in desktop and shutil.which("kde-open"):
subprocess.Popen(["kde-open", url])
return
webbrowser.open(url)
if __name__ == "__main__":
open_browser("https://example.com")