electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit bcd0e08dbceb0e910f1d839e12659c5d0e5d9754
parent 64cf87f91135ff07a619ec18d79cbeb277e1034c
Author: thomasv <thomasv@gitorious>
Date:   Thu, 12 Jan 2012 13:39:37 +0100

generic handler

Diffstat:
Mclient/electrum.py | 74+++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 41 insertions(+), 33 deletions(-)

diff --git a/client/electrum.py b/client/electrum.py @@ -228,16 +228,26 @@ class Interface: def set_port(self, port_number): self.port = port_number if self.use_http(): - import jsonrpclib - self.http_json_server = jsonrpclib.Server('http://%s:%d'%(self.host,self.port)) + self.handler = self.http_json_request + else: + self.handler = self.native_request def use_http(self): return self.port in [80,81,8080,8081] - def request(self, request ): + def native_request(self, method, params = ''): import time + cmds = {'session.new':'new_session', + 'peers':'peers', + 'session.poll':'poll', + 'session.update':'update_session', + 'blockchain.transaction.broadcast':'tx', + 'blockchain.address.get_history':'h' + } + cmd = cmds[method] + if type(params) != type(''): params = repr( params ) t1 = time.time() - request += "#" + request = repr ( (cmd, params) ) + "#" s = socket.socket( socket.AF_INET, socket.SOCK_STREAM) s.connect(( self.host, self.port)) s.send( request ) @@ -248,53 +258,51 @@ class Interface: else: break s.close() self.rtime = time.time() - t1 + if cmd == 'peers': + out = ast.literal_eval( out ) + return out + + def http_json_request(self, method, params = []): + import urllib2, json, time + if type(params) != type([]): params = [ params ] + t1 = time.time() + data = { 'method':method, 'id':'jsonrpc', 'params':params } + data_json = json.dumps(data) + host = 'http://%s:%d'%(self.host,self.port) + req = urllib2.Request(host, data_json, {'content-type': 'application/json'}) + response_stream = urllib2.urlopen(req) + response = json.loads( response_stream.read() ) + out = response.get('result') + if not out: + print response + self.rtime = time.time() - t1 return out def send_tx(self, data): - if self.use_http(): - out = self.http_json_server.blockchain.transaction.broadcast(data) - out = out.get("result") - else: - out = self.request( repr ( ('tx', data ))) + out = self.handler('blockchain.transaction.broadcast', data ) return out def retrieve_history(self, address): - if self.use_http(): - out = self.http_json_server.blockchain.address.get_history(address) - else: - out = ast.literal_eval( self.request( repr ( ('h', address ))) ) - return out + out = self.handler('blockchain.address.get_history', address ) + return ast.literal_eval( out ) def poll(self): - if self.use_http(): - out = ast.literal_eval( self.http_json_server.session.poll( self.session_id ) ) - else: - out = ast.literal_eval( self.request( repr ( ('poll', self.session_id )))) - - blocks, changed_addr = out + out = self.handler('session.poll', self.session_id ) + blocks, changed_addr = ast.literal_eval( out ) if blocks == -1: raise BaseException("session not found") self.blocks = int(blocks) return changed_addr def new_session(self, addresses, version): - if self.use_http(): - out = ast.literal_eval( self.http_json_server.session.new(addresses, version) ) - else: - out = ast.literal_eval( self.request( repr ( ('new_session', repr( ( version, addresses)) )))) - self.session_id, self.message = out + out = self.handler('session.new', [ version, addresses ] ) + self.session_id, self.message = ast.literal_eval( out ) def update_session(self, addresses): - if self.use_http(): - out = self.http_json_server.session.update(self.session_id, addresses) - else: - out = self.request( repr ( ('update_session', repr((self.session_id, addresses))))) + out = self.handler('session.update', [ self.session_id, addresses ] ) return out def get_servers(self): - if self.use_http(): - out = self.http_json_server.peers() - else: - out = ast.literal_eval( self.request( repr ( ('peers', '' )))) + out = self.handler('peers') self.servers = map( lambda x:x[1], out )