Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. import typing
  2. import json
  3. from http.server import BaseHTTPRequestHandler, HTTPServer
  4. import requests
  5.  
  6.  
  7. class JSONClient:
  8. def __init__(self, host, port):
  9. self.url = "http://{}:{}".format(host, port)
  10.  
  11. def call(self, method: str, params: typing.List=None, headers=None):
  12. payload = {
  13. "method": method,
  14. "params": params,
  15. "jsonrpc": "2.0",
  16. "id": 1,
  17. }
  18. return requests.post(self.url, data=json.dumps(payload), headers=headers).json()
  19.  
  20.  
  21. client = JSONClient('localhost', '18332')
  22.  
  23.  
  24. class JSONRPCServer(BaseHTTPRequestHandler):
  25. def do_POST(self):
  26. request = json.loads(self.rfile.read(int(self.headers['Content-Length'])).decode())
  27. result = {
  28. "id": request.get("id"),
  29. "result": None,
  30. "error": None
  31. }
  32. headers = {'content-type': 'application/json', 'Authorization': self.headers['Authorization']}
  33. print('Received Request %s' % request)
  34. response = client.call(request['method'], request['params'], headers=headers)
  35. if request['method'] == 'getblocktemplate':
  36. response['result']['transactions'] = [
  37. {
  38. "txid": "fb7a8658ec015133e36e2cf7ddf7e8c887c3a5becec2f30f24ebfe43e72f4b59",
  39. "data": "010000000a594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008a47304402200447ab31b9b503bf42f32ae6862fc5c8a204b317a0bc25fa64863dbe8df79bcc02200644bd09a48b2f2ed77bd5
  40. c961ed80e264529e44161fdcfe91756c169f28b5c701410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c
  41. 387c8e8f7ddf72c6ee3335101ec58867afb000000008a4730440220507820553c1ece1d85637bc56b1f569d486232b3f089a7a971a78de75de9ff2302204ecce80d8664b244a91ef4ece4c9b15caafa862e611e3464c53cd40370f5250f01410455fd1c1a6cbfb25b5bba
  42. 1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008b483045022100e4e2fad5e49
  43. 2d43028c24369f32ed401bff5b7b8091494645a85574f7f43d388022057e41e16887094ad81c7c693c6a91660cbfd6e03dcdb5f2db522c2a18718e20201410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df
  44. 2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008b483045022100fe79d350f6419f10097cb094fa783135c0bfe996e7bc33a766eb4db6bbebd4fd022078d9c0933
  45. 3fdf1be816b01271c819a29643550b119fed3fac0ee7f4b796bc3a101410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb24
  46. 0ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008b483045022100f8141cb972d989bb69dc40a07e3158ee02065c3040a3415c7e1cb0cb7b326c1602205f80c7b6f8517f0d14162980fb6f0f9b8564929bc34b541e014ca94756d4062901410455fd1
  47. c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008a47304402
  48. 2021a4cf7f40ea947694f5545f4f57492630ddbb710e85ed9f7b1eee330fdc068d02201db5766cf7d78666e3bbc9af42cdebb3e7a26bdb7c9affca36bdeae6c3fa972101410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147
  49. dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008b4830450221009cb14db295216a01820dc0d72fe26d824355ede8d72b722e514d114894c18ed5
  50. 022079a88ae3eecf6b2b943b9d227c4d26f37e999ecdc86e0edefd052706ca56b25c01410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594
  51. b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008b4830450221009850edd2d30c9a5f104ff645754a6bc40c2a18b40cd9cf2029fa6bc039b2b9a102206aae1b271a3f798bb263ddac931abe96cba37acf3756aa728c714e7b6a7476
  52. b701410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb00000
  53. 0008b483045022100db63a0753f6c2e88abfeb9f110777e64cbc0885d12b2ddcbee0f76a99186a2e4022024fc9e1c69c0d71c46ee406e838cf2d91893aef81a4d6c3767536c17a9ef44e401410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613
  54. 303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880c8ffffffff594b2fe743feeb240ff3c2cebea5c387c8e8f7ddf72c6ee3335101ec58867afb000000008a4730440220579435bc544e909cc65a6f628d8160ba7343b828278b4ea15db
  55. 05fe845cd3b16022002bfef77308bb3179cbf5e49e1635b87aa19ad49f1ec55f9414a4fa7d1a13d6f01410455fd1c1a6cbfb25b5bba1cf6f850de00d79852be3de51e50c0da683613303c533d079e147dfe07ce4d40df2b776b35184698d14fa107a61e0976b0d9416880
  56. c8ffffffff012018e505000000001976a914c8b876680fef08df5278a9df92df7e30b83cbb7188ac00000000",
  57. "hash": "fb7a8658ec015133e36e2cf7ddf7e8c887c3a5becec2f30f24ebfe43e72f4b59",
  58. "depends": [],
  59. "fee": 100000,
  60. "sigops": 80,
  61. "weight": 9040
  62. }
  63. ]
  64. result.update(response)
  65. self.send_response(200)
  66. self.send_header('Content-type', 'application/json')
  67. self.end_headers()
  68. self.wfile.write(json.dumps(response).encode())
  69.  
  70.  
  71. if __name__ == '__main__':
  72. HTTPServer(('localhost', 18161), JSONRPCServer).serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement