1

Okay, so I'm totally confused about what the hell is going on in my command prompt. I'm simply trying to make a script that will minimize the prompt window, open an IP address in the browser, and then run a simple HTTP server command via Python.

I have to run the HTTP server command AFTER the start command because if I run the HTTP server first, then the next commands DON'T RUN.

I even went down to installing some old custom .exe programs that added little features to fix things like this, but none of them worked correctly...


EDIT

I actually figured out it was "" that was causing it to not open the link, but it loads the site BEFORE the HTTP server starts. So my NEW question is: How do I run my http.server command BEFORE the start command without it not working (Running any command after an HTTP server command won't work since everything after it is never executed.)

EDIT again

Thanks again Anaksunaman for the answer, here is my final product for the command: (RunHTTPServer.bat):

@echo Off

start "" /min python -m http.server 8000 -b 127.0.0.1

sleep 1

start http://127.0.0.1:8000/

I added --bind to bind it to 127.0.0.1, as sometimes when using http.server, and trying to connect to Localhost:8000, it will say connection failed...

so that, OR remove -b/--bind and simply write your PERSONAL address in that start field.

6
  • That won't work. Command-Prompt cannot start HTTP. Have you tried start firefox.exe "http://ipAddress:Port" ? explorer "http://ipAddress:Port" may also work for DEFAULT Browser... – Ben Jun 24 '19 at 9:07
  • whats the code for starting chrome? – Mister SirCode Jun 24 '19 at 9:19
  • I guess: start chrome.exe "http://ipAddress:Port" or start "c:\program files\google chrome\chrome.exe http://ipAddress:Port". – Ben Jun 24 '19 at 9:36
  • No I was doing it wrong, i didnt need quotes, still it wont work because the website is loading before the http server works – Mister SirCode Jun 24 '19 at 9:40
  • Maybe you need a pause (e.g: timeout /T 5) before opening the browser [START-SERVER, PAUSE, START-BROWSER]. Or maybe your python program can start the browser when server-start has finished. – Ben Jun 24 '19 at 9:46
1

You can leverage the start command in both your calls. For example, using a batch file in Windows:

@echo Off

@rem Start two separate processes via the Windows "start" command.
@rem The second command to open the browser will not be blocked.

@rem start the Python HTTP server with a minimized command window ("/min").
start "" /min python -m http.server 8000 --bind 127.0.0.1

@rem Give the HTTP server a second to start (optional).
sleep 1

@rem Open Firefox to the Python HTTP server instance.
start "" firefox http://localhost:8000

Or you can use the almost the exact same commands in Python with the subprocess module:

#!/Programs/Python37/python

# Start two separate processes via the Windows "start" command.
# The second command to open the browser will not be blocked.

import sys
import subprocess
import time

# "/min" (below) minimizes the spawned command window (Windows).
try:

    subprocess.run('cmd /c start "" /min python -m http.server 8000 --bind 127.0.0.1')
    # subprocess.run('cmd /c start "" /min python http-server.py')

    # Wait for our server to start (optional)
    time.sleep(1)

    # Open our browser
    subprocess.run('cmd /c start firefox http://localhost:8000')

except Exception as err:
    print('')
    print(err)
    sys.exit()

On a side note, as you correctly point out in the comments below, you can omit the browser name (e.g. firefox) in your start command to simply open the website in the default browser for Windows (i.e. start "" http://localhost:8000 or even just start http://localhost:8000).

8
  • wait theres a /min built into windows? – Mister SirCode Jul 1 '19 at 7:37
  • Technically, it's an option to the start command. – Anaksunaman Jul 1 '19 at 7:42
  • wow, so why is everyone online saying you require external programs for it? perhaps they didnt know? weird. Well thanks. – Mister SirCode Jul 1 '19 at 7:48
  • There can be advantages to using other programs or methods, depending on your goals. But for immediately minimizing a window, /min works pretty well. In any case, you're welcome. Glad to help. – Anaksunaman Jul 1 '19 at 8:04
  • Also Anaksunaman, Consider adding --bind 127.0.0.1 to your python command, or just -b 127.0.0.1 like i did in my edit on my question above... As it helps make sure your browser runs localhost. sometimes in python (for unknown reasons) it binds to 0.0.0.0 rather than localhost by default. – Mister SirCode Jul 1 '19 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.