From 2d410a4b13c79e662a262a26590ad926d1f0f0ae Mon Sep 17 00:00:00 2001 From: kris Date: Thu, 14 Mar 2019 21:38:12 +0000 Subject: [PATCH] Take filename to serve from argv. --- server.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/server.py b/server.py index fdebc1b..b2139eb 100644 --- a/server.py +++ b/server.py @@ -1,22 +1,26 @@ import socketserver +import sys ADDR = "192.168.1.15" PORT = 20000 -FILE = "out.bin" +def main(argv): + serve_file = argv[1] -class ChunkHandler(socketserver.BaseRequestHandler): - def handle(self): - with open(FILE, "rb") as f: - data = f.read() - print("Sending %d bytes" % len(data)) - self.request.sendall(data) + def handler(serve_file): + class ChunkHandler(socketserver.BaseRequestHandler): + def handle(self): + with open(serve_file, "rb") as f: + data = f.read() + print("Sending %d bytes" % len(data)) + self.request.sendall(data) + return ChunkHandler -def main(): with socketserver.TCPServer( - (ADDR, PORT), ChunkHandler, bind_and_activate=False) as server: + (ADDR, PORT), handler(serve_file), + bind_and_activate=False) as server: server.allow_reuse_address = True server.server_bind() server.server_activate() @@ -24,4 +28,4 @@ def main(): if __name__ == "__main__": - main() + main(sys.argv)