Search code examples
javascriptpythonsendfile

Python Show webpage on connect to server


I built a server using JavaScript that displayed a webpage when you connected to it. The server sent the htm file upon connection using

app.get('/', function (req, res) {
    res.sendfile('index.htm');
});

Is there a similar function in Python?


Solution

  • import SimpleHTTPServer
    import SocketServer
    
    PORT = 8000
    
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    

    see https://docs.python.org/2/library/simplehttpserver.html