#!/usr/bin/env python3 """POST echo endpoint for the compatibility ladder's 036a page. Run this on the host, then start QEMU with a user-mode NIC: python3 testdata/browser/compat/post_server.py qemu-system-i386 ... -netdev user,id=n0 -device ne2k_pci,netdev=n0 036a posts `alpha=two&beta=on` to http://10.0.2.2:8765/echo and expects the response page to say PASS. The guest uses QEMU's slirp host address, so the same script works for `make run-net` (use the host's LAN address instead). """ from http.server import BaseHTTPRequestHandler, HTTPServer PORT = 8765 class Handler(BaseHTTPRequestHandler): def do_POST(self): length = int(self.headers.get('Content-Length', '0') or 0) data = self.rfile.read(length).decode('latin-1') ok = (self.path == '/echo' and data == 'alpha=two&beta=on') verdict = 'PASS: server received POST' if ok else 'FAIL: body=' + data body = ( '
' '' + verdict + '
' '' + data.replace('&', '&').replace('<', '<') +
''
).encode('latin-1')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
body = b'POST test endpoint'
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, fmt, *args):
pass
if __name__ == '__main__':
HTTPServer(('0.0.0.0', PORT), Handler).serve_forever()