-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrader.py
executable file
·93 lines (80 loc) · 3.81 KB
/
trader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python3
# This module takes all the services required for colored trading and unites
# them under one API, as a JSON-RPC server that can be accessed from a browser.
# Since we are going to run some processes, let's make sure we exit cleanly
from atexit import register as addtoexit
def close():
print("\nshutting down")
color.close()
message.close()
bitcoin.close()
addtoexit(close)
# Then we can import the actual functionality
import color, message, bitcoin
# And run a server that exposes it
if __name__ == '__main__':
# Create a cross domain request handler (with OPTIONS request) that serves
# JSON on post and JSONp + files on GET
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler
from http.server import SimpleHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
from json import dumps, loads
class Handler(SimpleHTTPRequestHandler, SimpleJSONRPCRequestHandler):
def do_OPTIONS(self):
self.send_response(200, 'ok')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type')
self.send_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'X-Requested-With')
self.end_headers()
def do_GET(self):
# Parse query string, make sure we have a callback.
url = urlparse(self.path)
if '.jsonp' != url.path[-6:]: return SimpleHTTPRequestHandler.do_GET(self)
query = parse_qs(url.query)
if 'callback' not in query: raise Exception('No callback specified')
callback = query['callback'][-1]
# Get data for different JSONp calls
try:
if '/colorvalue.jsonp' == url.path:
data = color.colorvalue(query['colordef'][0], query['txo[]'])
elif '/makeconversion.jsonp' == url.path:
data = color.makeconversion(loads(query['txspec'][0]))
elif '/receive.jsonp' == url.path:
data = message.receive()
elif '/send.jsonp' == url.path:
data = message.send(query['subject'][0], query['body'][0])
elif '/signrawtransaction.jsonp' == url.path:
data = bitcoin.signrawtransaction(
query['rawtx'][0],
loads(query['inputs'][0]),
query['keys[]']
)
else:
data = {'error': 'Did not understand ' + url.path}
except (KeyError, ValueError): data = {'error': 'Wrong parameters', 'query': query}
# Send the reply as jsonp
self.send_response(200)
self.send_header('Content-type', 'application/javascript')
self.end_headers()
self.wfile.write(bytes(callback + '(' + dumps(data) + ');', 'UTF-8'))
# Create server
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
server = SimpleJSONRPCServer(('', 6710), Handler)
# Register functions
server.register_introspection_functions()
server.register_function(color.colorvalue, 'colorvalue')
server.register_function(color.makeconversion, 'makeconversion')
server.register_function(message.send, 'send')
server.register_function(message.receive, 'receive')
server.register_function(bitcoin.signrawtransaction, 'signrawtransaction')
# Serve RPC till exit
addtoexit(server.shutdown)
sockname = server.socket.getsockname()
print("\nServing JSON, JSONp and HTTP on", sockname[0], "port", sockname[1], "...")
try:
server.serve_forever()
# Shut down
except KeyboardInterrupt:
from sys import exit
exit(0)