update twisted/demo.py
This commit is contained in:
parent
aec8b65724
commit
8f1ce1f835
|
@ -1,28 +1,38 @@
|
||||||
import sys
|
import sys, json
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
from .transcribe import SymmetricWormhole
|
from .transcribe import Wormhole
|
||||||
from .. import public_relay
|
from .. import public_relay
|
||||||
|
|
||||||
APPID = "lothar.com/wormhole/text-xfer"
|
APPID = "lothar.com/wormhole/text-xfer"
|
||||||
|
|
||||||
w = SymmetricWormhole(APPID, public_relay.RENDEZVOUS_RELAY)
|
w = Wormhole(APPID, public_relay.RENDEZVOUS_RELAY)
|
||||||
|
|
||||||
if sys.argv[1] == "send-text":
|
if sys.argv[1] == "send-text":
|
||||||
message = sys.argv[2]
|
message = sys.argv[2]
|
||||||
|
data = json.dumps({"message": message}).encode("utf-8")
|
||||||
d = w.get_code()
|
d = w.get_code()
|
||||||
def _got_code(code):
|
def _got_code(code):
|
||||||
print "code is:", code
|
print "code is:", code
|
||||||
return w.get_data(message)
|
return w.get_data(data)
|
||||||
d.addCallback(_got_code)
|
d.addCallback(_got_code)
|
||||||
def _got_data(their_data):
|
def _got_data(them_bytes):
|
||||||
print "ack:", their_data
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
||||||
|
if them_d["message"] == "ok":
|
||||||
|
print "text sent"
|
||||||
|
else:
|
||||||
|
print "error sending text: %r" % (them_d,)
|
||||||
d.addCallback(_got_data)
|
d.addCallback(_got_data)
|
||||||
elif sys.argv[1] == "receive-text":
|
elif sys.argv[1] == "receive-text":
|
||||||
code = sys.argv[2]
|
code = sys.argv[2]
|
||||||
w.set_code(code)
|
w.set_code(code)
|
||||||
d = w.get_data("ok")
|
data = json.dumps({"message": "ok"}).encode("utf-8")
|
||||||
def _got_data(their_data):
|
d = w.get_data(data)
|
||||||
print their_data
|
def _got_data(them_bytes):
|
||||||
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
||||||
|
if "error" in them_d:
|
||||||
|
print >>sys.stderr, "ERROR: " + them_d["error"]
|
||||||
|
return 1
|
||||||
|
print them_d["message"]
|
||||||
d.addCallback(_got_data)
|
d.addCallback(_got_data)
|
||||||
else:
|
else:
|
||||||
raise ValueError("bad command")
|
raise ValueError("bad command")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user