Welcome, guest | Sign In | My Account | Store | Cart
import BaseHTTPServer
import webbrowser

def LoadInDefaultBrowser(html):
    """Display html in the default web browser without creating a temp file.

    Instantiates a trivial http server and calls webbrowser.open with a URL
    to retrieve html from that server.
    """

    class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        def do_GET(self):
            bufferSize = 1024*1024
            for i in xrange(0, len(html), bufferSize):
                self.wfile.write(html[i:i+bufferSize])

    server = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), RequestHandler)
    webbrowser.open('http://127.0.0.1:%s' % server.server_port)
    server.handle_request()

if __name__ == '__main__':
    LoadInDefaultBrowser('<b>Hello World</b>')

History

  • revision 3 (17 years ago)
  • previous revisions are not available