okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
714ff8242
main
1#!/usr/bin/env python3 2"""cutserver.py PORT DIR [NAMES BYTES]: serve DIR over http on 127.0.0.1; each file called in NAMES (comma separated) is cut after BYTES bytes (the connection breaks part-way), 3every other file is served whole. For the real-package flow tests (update_model_flow.sh): a download that fails. Standard library only.""" 4import functools 5import socketserver 6import sys 7from http .server import SimpleHTTPRequestHandler ,ThreadingHTTPServer 8 9 10class Server (ThreadingHTTPServer ): 11"""http.server's server_bind also asks socket.getfqdn for the address, which stalled 35 s in every new process on GitHub's macOS runner.""" 12 13def server_bind (self ): 14socketserver .TCPServer .server_bind (self ) 15self .server_name ,self .server_port = self .server_address [:2 ] 16 17 18class Handler (SimpleHTTPRequestHandler ): 19cut_names = [] 20cut_bytes = 0 21 22def do_GET (self ): 23if self .path .rsplit ("/" ,1 )[- 1 ]in self .cut_names : 24path = self .translate_path (self .path ) 25with open (path ,"rb" )as f : 26size = f .seek (0 ,2 ) 27f .seek (0 ) 28data = f .read (self .cut_bytes ) 29self .send_response (200 ) 30self .send_header ("Content-Length" ,str (size )) 31self .send_header ("Content-Type" ,"application/gzip" ) 32self .end_headers () 33self .wfile .write (data ) 34self .wfile .flush () 35self .close_connection = True 36return 37super ().do_GET () 38 39def log_message (self ,fmt ,* args ): 40sys .stderr .write ("%s - %s\n" % (self .address_string (),fmt % args )) 41 42 43def main (): 44port ,directory = int (sys .argv [1 ]),sys .argv [2 ] 45Handler .cut_names = (sys .argv [3 ]if len (sys .argv )> 3 else "" ).split ("," ) 46Handler .cut_bytes = int (sys .argv [4 ])if len (sys .argv )> 4 else 0 47server = Server (("127.0.0.1" ,port ),functools .partial (Handler ,directory = directory )) 48server .serve_forever () 49 50 51if __name__ == "__main__" : 52main ()