Browse Source

handle http

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

39
client/electrum.py

@ -17,7 +17,7 @@
# 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:
import ecdsa
@ -396,24 +396,27 @@ class Wallet:
return conf, unconf
def request(self, request ):
import urllib
if self.port == 80:
try:
out = urllib2.urlopen('http://'+self.host+'/q/tw', request, timeout=5).read()
except:
out = ''
request2 = urllib.urlencode({'q':request})
request = "GET /electrum.php?" + request2 + " HTTP/1.0\r\n\r\n"
else:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
s.connect(( self.host, self.port))
s.send(request)
out = ''
while 1:
msg = s.recv(1024)
if msg: out += msg
else: break
s.close()
if re.match('[^:]\s*\(', out): out = ''
request += "#"
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
s.connect(( self.host, self.port))
s.send( request )
out = ''
while 1:
msg = s.recv(1024)
if msg: out += msg
else: break
s.close()
if self.port == 80:
out = out.split('\r\n')[-1]
return out
def retrieve_message(self):
@ -427,10 +430,10 @@ class Wallet:
return ast.literal_eval( self.request( repr ( ('h', address ))) )
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):
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):
blocks, changed_addresses = self.poll()

41
server/server.py

@ -289,31 +289,42 @@ def listen_thread(store):
def client_thread(ipaddr,conn):
print "client thread", ipaddr
try:
ipaddr = ipaddr[0]
msg = ''
while 1:
d = conn.recv(1024)
if d: msg+=d
try:
cmd, data = ast.literal_eval(msg)
msg += d
if d[-1]=='#':
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':
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)
sessions[ipaddr] = {}
sessions[session_id] = {}
for a in addresses:
sessions[ipaddr][a] = ''
out = SERVER_MESSAGE
sessions[session_id][a] = ''
out = repr( (session_id, SERVER_MESSAGE) )
elif cmd=='poll':
addresses = sessions.get(ipaddr)
session_id = data
addresses = sessions.get(session_id)
if not addresses:
print "session not found", ipaddr
out = repr( (-1, {}))
@ -321,9 +332,9 @@ def client_thread(ipaddr,conn):
ret = {}
for addr in addresses:
status = store.get_status( addr )
last_status = sessions[ipaddr].get( addr )
last_status = sessions[session_id].get( addr )
if last_status != status:
sessions[ipaddr][addr] = status
sessions[session_id][addr] = status
ret[addr] = status
out = repr( (store.get_block_number(1), ret ) )
@ -386,7 +397,9 @@ if __name__ == '__main__':
args.connect_args = {"database":"abe"}
store = MyStore(args)
thread.start_new_thread(listen_thread, (store,))
#thread.start_new_thread(listen_thread, (store,))
listen_thread(store)
exit(0)
while True:
try:

Loading…
Cancel
Save