Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 41
HTTPServer
Beau Barker edited this page Jul 29, 2025
·
2 revisions
Demonstrates using Python's builtin http.server module to serve JSON-RPC.
fromhttp.serverimportBaseHTTPRequestHandler, HTTPServerfromjsonrpcserverimportResult, Success, dispatch, method@methoddefping() ->Result:
"""JSON-RPC method"""returnSuccess("pong")
classTestHttpServer(BaseHTTPRequestHandler):
"""HTTPServer request handler"""defdo_POST(self) ->None: # pylint: disable=invalid-name"""POST handler"""# Process requestrequest=self.rfile.read(int(self.headers["Content-Length"])).decode()
response=dispatch(request)
# Return responseself.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(response.encode())
if__name__=="__main__":
HTTPServer(("localhost", 5000), TestHttpServer).serve_forever()Reference: JSON-RPC in HTTPServer.
Contributions are appreciated – simply hit Edit or New page.