Skip to content

Commit

Permalink
Refactor file_server to run in a netns
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaswal committed Dec 3, 2024
1 parent 168647a commit a04ad3f
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions test/infamy/file_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@
import http.server
import socket


class FileServer(http.server.HTTPServer):
class FileServer:
"""Open web server on (address, port) serving files from directory"""
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def log_message(self, *args, **kwargs):
pass
def __init__(self, netns, server_address, directory):
self.server_address = server_address
self.directory = directory
self.netns = netns
self.process = None

address_family = socket.AF_INET6
def start(self):
cmd = f"httpd -p {self.server_address[0]}:{self.server_address[1]} -f -h {self.directory}"
self.process = self.netns.popen(cmd.split(" "))

def __init__(self, server_address, directory):
rh = functools.partial(FileServer.RequestHandler, directory=directory)
self.__tp = concurrent.futures.ThreadPoolExecutor(max_workers=1)
super().__init__(server_address, rh)
def stop(self):
if self.process:
self.process.terminate()
self.process.wait()
self.process = None

def __enter__(self):
self.__tp.submit(self.serve_forever)
self.start()

def __exit__(self, _, __, ___):
self.shutdown()
self.__tp.shutdown()
self.stop()

0 comments on commit a04ad3f

Please sign in to comment.