How do I setup a portable pydoc server that closes when portable browsers have been shut down?

StackOverflow https://stackoverflow.com/questions/4293701

  •  28-09-2019
  •  | 
  •  

Question

I've set up a portable environment on my flash drive consisting of Chromium Portable, Firefox Portable and Python 3. I am trying to write a Python 3 script that will launch a pydoc server to be accessed either in Chromium Portable or Firefox Portable. If Chromium Portable is already active, a new tab to the server will be opened, if not Firefox Portable will be launched and the server will be accessed from there. When all processes of Chromium Portable\chrome.exe and Firefox Portable have been closed, the server should be shut down. The following code is what I have written so far.

import subprocess
import os 

drive = os.environ['HOMEDRIVE']
def launch_browser():
    pass

def launch_pydoc():
    pythonw = drive + '\PortableApps\PortablePython_3x\App\pythonw.exe'
    doc_args = drive + '\PortableApps\PortablePython_3x\App\Lib\pydoc.py -p 50000'
    spDoc = subprocess.Popen(pythonw+' '+doc_args)

def launch_chrome():
    chrome = drive + '\PortableApps\ChromiumPortable\App\Chrome-bin\chrome.exe'
    chrome_args = r'http://localhost:50000/ --user-data-dir="C:\Users\Owner\AppData\Roaming\ChromePortable\nathan" --new-tab'
    spChrome = subprocess.Popen(chrome+' '+chrome_args)

def launch_mozilla():
    mozilla = drive + '\PortableApps\FirefoxPortableNightly\FirefoxPortable.exe'
    mozilla_args = 'http://localhost:50000/'
    spMozilla = subprocess.Popen(mozilla+' '+mozilla_args)

This script is launched from a batch file that sets the value of HOMEDRIVE to the current flash drive letter so Python will always know where to look for the executables. My problem is that I'm not sure how to implement the process that will determine whether to simply open a new tab in Chromium Portable or to open Firefox Portable and furthermore to shut down the server after each process of whichever browser has been closed. I have looked at many links pertaining to subprocess etc especially how to wait for multiple child processes, how to kill a subprocess and ensuring subprocesses are dead on exiting python program. I have installed pywin32, wmi, comtypes and psutil packages in my copy of Python 3. Help please? If it is necessary for my code to be discarded it is OK. I am trying to learn. Thanks in advance.

Was it helpful?

Solution

In principle, you should be able to use the wait() method of your Popen objects. Something like this:

pydoc_process = launch_pydoc()
browser_process = launch_browser()
browser_process.wait()
pydoc_process.terminate()

(Modify your launch functions to end with return subprocess.Popen(...)

But I don't know how well that will work with modern browsers. In Linux, if Firefox is called to open an address, and it detects you've already got Firefox open, the new process hands the URL to the old process, and exits immediately. That sort of thing would mess up what you're trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top