Browse Source

handle http

283
ThomasV 13 years ago
parent
commit
d315a413c7
  1. 39
      client/electrum.py
  2. 39
      server/server.py

39
client/electrum.py

@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, base64, os, re, hashlib, socket, getpass, copy, operator, urllib2, ast import sys, base64, os, re, hashlib, socket, getpass, copy, operator, ast
try: try:
import ecdsa import ecdsa
@ -396,24 +396,27 @@ class Wallet:
return conf, unconf return conf, unconf
def request(self, request ): def request(self, request ):
import urllib
if self.port == 80: if self.port == 80:
try: request2 = urllib.urlencode({'q':request})
out = urllib2.urlopen('http://'+self.host+'/q/tw', request, timeout=5).read() request = "GET /electrum.php?" + request2 + " HTTP/1.0\r\n\r\n"
except:
out = ''
else: else:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM) request += "#"
s.connect(( self.host, self.port))
s.send(request) s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
out = '' s.connect(( self.host, self.port))
while 1: s.send( request )
msg = s.recv(1024) out = ''
if msg: out += msg while 1:
else: break msg = s.recv(1024)
s.close() if msg: out += msg
else: break
if re.match('[^:]\s*\(', out): out = '' s.close()
if self.port == 80:
out = out.split('\r\n')[-1]
return out return out
def retrieve_message(self): def retrieve_message(self):
@ -427,10 +430,10 @@ class Wallet:
return ast.literal_eval( self.request( repr ( ('h', address ))) ) return ast.literal_eval( self.request( repr ( ('h', address ))) )
def poll(self): def poll(self):
return ast.literal_eval( self.request( repr ( ('poll', '' )))) return ast.literal_eval( self.request( repr ( ('poll', self.session_id ))))
def new_session(self): def new_session(self):
self.message = self.request( repr ( ('watch', repr(self.addresses) ))) self.session_id, self.message = ast.literal_eval( self.request( repr ( ('session', repr(self.addresses) ))))
def update(self): def update(self):
blocks, changed_addresses = self.poll() blocks, changed_addresses = self.poll()

39
server/server.py

@ -289,31 +289,42 @@ def listen_thread(store):
def client_thread(ipaddr,conn): def client_thread(ipaddr,conn):
print "client thread", ipaddr
try: try:
ipaddr = ipaddr[0] ipaddr = ipaddr[0]
msg = '' msg = ''
while 1: while 1:
d = conn.recv(1024) d = conn.recv(1024)
if d: msg+=d msg += d
try: if d[-1]=='#':
cmd, data = ast.literal_eval(msg)
break break
except:
continue
print msg
try:
cmd, data = ast.literal_eval(msg[:-1])
except:
print "syntax error", repr(msg)
conn.close()
return
if cmd=='b': if cmd=='b':
out = "%d"%store.get_block_number(1) out = "%d"%store.get_block_number(1)
elif cmd=='watch': elif cmd=='session':
import random, string
session_id = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
print "new session", ipaddr, session_id
addresses = ast.literal_eval(data) addresses = ast.literal_eval(data)
sessions[ipaddr] = {} sessions[session_id] = {}
for a in addresses: for a in addresses:
sessions[ipaddr][a] = '' sessions[session_id][a] = ''
out = SERVER_MESSAGE out = repr( (session_id, SERVER_MESSAGE) )
elif cmd=='poll': elif cmd=='poll':
addresses = sessions.get(ipaddr) session_id = data
addresses = sessions.get(session_id)
if not addresses: if not addresses:
print "session not found", ipaddr print "session not found", ipaddr
out = repr( (-1, {})) out = repr( (-1, {}))
@ -321,9 +332,9 @@ def client_thread(ipaddr,conn):
ret = {} ret = {}
for addr in addresses: for addr in addresses:
status = store.get_status( addr ) status = store.get_status( addr )
last_status = sessions[ipaddr].get( addr ) last_status = sessions[session_id].get( addr )
if last_status != status: if last_status != status:
sessions[ipaddr][addr] = status sessions[session_id][addr] = status
ret[addr] = status ret[addr] = status
out = repr( (store.get_block_number(1), ret ) ) out = repr( (store.get_block_number(1), ret ) )
@ -386,7 +397,9 @@ if __name__ == '__main__':
args.connect_args = {"database":"abe"} args.connect_args = {"database":"abe"}
store = MyStore(args) store = MyStore(args)
thread.start_new_thread(listen_thread, (store,)) #thread.start_new_thread(listen_thread, (store,))
listen_thread(store)
exit(0)
while True: while True:
try: try:

Loading…
Cancel
Save