import http.server
from logging import shutdown
import socketserver
import webbrowser
import sys
import os

if __name__ == "__main__":
    Handler = http.server.SimpleHTTPRequestHandler
    args = sys.argv
    if len(args) < 3:
            print("Warning: You should set the directry path on the 2nd args")
            print("       : , and the URL on the 3rd args for launching the web page")
    port = int(args[3])
    with socketserver.TCPServer(("", port), Handler) as httpd:
        print("serving at port", port)
        os.chdir(args[1])
        url = args[2]
        try:
            webbrowser.open(url, new=0, autoraise=True)
            httpd.serve_forever()
        except KeyboardInterrupt:
            httpd.shutdown()
            print("close!")
        httpd.server_close()

        def Shutdown():
            httpd.shutdown()
        