1
0
mirror of https://github.com/tzuwei-huang/random-number-generator.git synced 2026-08-24 14:10:29 +09:00
Tzu-Wei, Huang 24d3f49101
feat: add pure random function (#1)
* - Add "Pure Random" mode with repeat detection feature
- Improve UI layout and styling for compact presentation screens
- Introduce auto cleanup of existing processes on port 80 before startup
- Update translations and language toggle functionalities
- Remove unused test dependencies
- Add Apache 2.0 license and update relevant metadata

* fix: Update number pooling logic and adjust test for remaining count
2026-01-24 17:07:07 +09:00

48 lines
1.6 KiB
Python

import webbrowser
import uvicorn
import sys
import ctypes
import os
import subprocess
from threading import Timer
from rng_app.app import app
def open_browser():
webbrowser.open_new("http://127.0.0.1")
def kill_existing_process(port=80):
try:
# Use errors='replace' to handle potential encoding issues on Windows
output = subprocess.check_output(f'netstat -ano | findstr :{port}', shell=True).decode(errors='replace')
for line in output.splitlines():
if f':{port}' in line and 'LISTENING' in line:
pid = line.strip().split()[-1]
if int(pid) > 0 and int(pid) != os.getpid():
subprocess.call(f'taskkill /F /PID {pid}', shell=True)
except Exception:
pass
try:
current_pid = os.getpid()
# Use errors='replace' and handle potential exceptions
output = subprocess.check_output('tasklist /FI "IMAGENAME eq RandomPicker.exe" /FO CSV /NH', shell=True).decode(errors='replace')
for line in output.splitlines():
if line.strip():
parts = line.split(',')
if len(parts) > 1:
pid = parts[1].strip('"')
if pid.isdigit() and int(pid) != current_pid:
subprocess.call(f'taskkill /F /PID {pid}', shell=True)
except Exception:
pass
if __name__ == "__main__":
kill_existing_process(80)
Timer(1, open_browser).start()
try:
uvicorn.run(app, host="127.0.0.1", port=80, log_config=None)
except Exception:
open_browser()
sys.exit(1)