Spaces:
Running
Running
Download server_observation.py from WideChaos/http-range-desk: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/WideChaos/http-range-desk/resolve/main/server_observation.py
- Command line
-
hf download hf://spaces/WideChaos/http-range-desk/server_observation.py
-
curl -L -o server_observation.py https://huggingface.co/spaces/WideChaos/http-range-desk/resolve/main/server_observation.py
2.61 kB
| """A real stdlib server observation: optional Range support must not be assumed.""" | |
| from pathlib import Path | |
| from http.server import ThreadingHTTPServer,SimpleHTTPRequestHandler | |
| from functools import partial | |
| import threading,tempfile,http.client,os,json,hashlib,email.utils | |
| P=Path(__file__).resolve().parent;results=[];data=bytes(range(256));modified=784111777 | |
| class Quiet(SimpleHTTPRequestHandler): | |
| def log_message(self,*args):pass | |
| with tempfile.TemporaryDirectory(prefix='range optional server ') as d: | |
| root=Path(d);(root/'sample.bin').write_bytes(data);(root/'empty.bin').write_bytes(b'');os.utime(root/'sample.bin',(modified,modified));os.utime(root/'empty.bin',(modified,modified));server=ThreadingHTTPServer(('127.0.0.1',0),partial(Quiet,directory=d));t=threading.Thread(target=server.serve_forever,daemon=True);t.start() | |
| try: | |
| for method,path,headers in [('GET','/sample.bin',{'Range':'bytes=0-3'}),('GET','/sample.bin',{'Range':'bytes=9999-'}),('GET','/sample.bin',{'Range':'bytes=0-0,-1'}),('GET','/sample.bin',{'Range':'bytes=0-3','If-Range':'"resource-v1"'}),('HEAD','/sample.bin',{'Range':'bytes=0-3'}),('GET','/empty.bin',{'Range':'bytes=-1'}),('GET','/sample.bin',{'Range':'bytes=0-3','If-Modified-Since':email.utils.formatdate(modified,usegmt=True)})]: | |
| c=http.client.HTTPConnection('127.0.0.1',server.server_port,timeout=5);c.request(method,path,headers=headers);r=c.getresponse();body=r.read();rows={'method':method,'path':path,'request_headers':headers,'status':r.status,'content_range':r.getheader('Content-Range'),'content_length':r.getheader('Content-Length'),'last_modified':r.getheader('Last-Modified'),'actual_body_bytes':len(body),'actual_body_sha256':hashlib.sha256(body).hexdigest()};c.close();results.append(rows) | |
| assert [r['status'] for r in results]==[200,200,200,200,200,200,304] | |
| assert all(r['content_range'] is None for r in results) | |
| assert [r['actual_body_bytes'] for r in results]==[256,256,256,256,0,0,0] | |
| finally:server.shutdown();server.server_close();t.join(timeout=5) | |
| (P/'server-observation.json').write_text(json.dumps({'status':'PASS','actual_local_http_requests':7,'implementation':'Python standard-library SimpleHTTPRequestHandler, current recorded environment','findings':'This server ignores Range; a profile 206/416 plan does not predict every server. HEAD and other conditional outcome are separate.','not_a_universal_HTTP_certification':True,'all_server_resources_stopped':not t.is_alive(),'observations':results},indent=2)+'\n');print({'actual_requests':len(results),'server_stopped':not t.is_alive(),'statuses':[r['status'] for r in results]}) | |