Skip to content

Instantly share code, notes, and snippets.

@chfast
Created November 29, 2017 17:32
Show Gist options
  • Save chfast/78516bfc9cf7799fe4140ab9a0fe07c6 to your computer and use it in GitHub Desktop.
Save chfast/78516bfc9cf7799fe4140ab9a0fe07c6 to your computer and use it in GitHub Desktop.
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(b"JSON-RPC Proxy\n")
def do_POST(self):
self.log_message("Headers: {}".format(self.headers))
request_length = int(self.headers['Content-Length'])
self.log_message("JSON RPC request length: {}".format(request_length))
request_content = self.rfile.read(request_length)
self.log_message("JSON RPC request: {}".format(request_content))
request = json.loads(request_content.decode('ascii'))
method = request["method"]
self.log_message("method: {}".format(method))
response = {'jsonrpc': "2.0", 'id': request["id"]}
if method == 'rpc_modules':
response['result'] = {'eth': '1.0'}
response_content = json.dumps(response)
self.log_message("response: {}".format(response_content))
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(response_content.encode('ascii'))
def run(server_class=HTTPServer, handler_class=HTTPRequestHandler):
server_address = ('127.0.0.1', 8545)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment