Skip to content Skip to sidebar Skip to footer

Simple Http Server Does Not Work When Converted To Exe

I wrote a simple python http server to serve the files(folders) of the present working directory. import socketserver http='' def httpServer(hostIpAddress): global http socket

Solution 1:

Instead of using a function httpserver , I used class and it build the exe without any problem and now the http server runs even in its executable form.

Credit to: https://stackoverflow.com/users/642070/tdelaney for providing this solution at :

https://pastebin.com/KsTmVWRZ

import http.server
import threading
import functools
import time
 
# Example simple http server as threadclassSilentHandler(http.server.SimpleHTTPRequestHandler):
    
    deflog_message(self, format, *args, **kwargs):
        # do any logging you like therepassclassMyHttpServerThread(threading.Thread):
    
    def__init__(self, address=("0.0.0.0",8000), target_dir="."):
        super().__init__()
        self.address = address
        self.target_dir = "."
        self.server = http.server.HTTPServer(address, functools.partial(SilentHandler, directory=self.target_dir))
        self.start()
 
    defrun(self):
        self.server.serve_forever(poll_interval=1)
 
    defstop(self):
        self.server.shutdown() # don't call from this thread# test  if __name__ == "__main__":
    http_server = MyHttpServerThread()
    time.sleep(10)
    http_server.stop()
    print("done")

Post a Comment for "Simple Http Server Does Not Work When Converted To Exe"