8

I'm trying to start a simple HTTP server then open it in the default browser. I don't know what I'm doing wrong, it's either not starting the server at all, or it's stopping as soon as it gets to the end of the script (isn't it supposed to run forever?).

import BaseHTTPServer, SimpleHTTPServer, webbrowser, thread
def start_server():
  httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 3600), SimpleHTTPServer.SimpleHTTPRequestHandler)
  httpd.serve_forever()
thread.start_new_thread(start_server,())
url = 'http://127.0.0.1:3600'
webbrowser.open_new(url)
4
  • How are you running this script? What is displayed in the browser -- do you get a positive error response from the server (i.e. 404 Not Found), or do you get a timeout, or some other error? Commented Jul 11, 2018 at 23:19
  • saved as a file and calling through file.py on cmd on windows. it looks like it runs through the script and then opens the browser, which says the site can't be reached. Commented Jul 11, 2018 at 23:21
  • Do you mean you're running it with python file.py? Commented Jul 11, 2018 at 23:23
  • yeah, python file.py. i omitted that in my previous comment. Commented Jul 11, 2018 at 23:24

2 Answers 2

8

A thread continues to exist as long as the application continues to run, in the case webbrowser.open_new() is not blocking so the browser will hardly finish running the application, what you should do is make a blocker to prevent the application finish of execute:

import sys
import thread
import webbrowser
import time

import BaseHTTPServer, SimpleHTTPServer

def start_server():
    httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 3600), SimpleHTTPServer.SimpleHTTPRequestHandler)
    httpd.serve_forever()

thread.start_new_thread(start_server,())
url = 'http://127.0.0.1:3600'
webbrowser.open_new(url)

while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        sys.exit(0)
Sign up to request clarification or add additional context in comments.

2 Comments

that's what I was looking for, thanks! where can I read more about this method of blocking?
@fadelm0 That is basic theory in concurrent programming. Think the script is sequential, if it finishes executing it will eliminate all its resources as the created threads. If my answer helps you, do not forget to mark it as correct :)
7

Here is a Python 3 version of the answer by @eyllanesc using the newer http.server and the threading module

import sys
import time
import threading
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler

ip = "127.0.0.1"
port = 3600
url = f"http://{ip}:{port}"


def start_server():
    server_address = (ip, port)
    httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
    httpd.serve_forever()


threading.Thread(target=start_server).start()
webbrowser.open_new(url)

while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        sys.exit(0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.