Permalink
Browse files

adds functionality for connections opening upon running the script ra…

…ther than the first message sent
  • Loading branch information...
1 parent de5e2ab commit ab42f1747ea7ad8dc02576e261d0a5625fb9b700 @barberdt barberdt committed Nov 29, 2011
Showing with 18 additions and 5 deletions.
  1. +18 −5 produce.py
View
@@ -28,19 +28,31 @@ def produce(hostname, api_key, swarm_id, resource_id, wrap):
conn.putheader("x-bugswarmapikey", api_key)
conn.putheader("transfer-encoding", "chunked")
conn.endheaders()
-
+
#Sleep required to allow the swarm server time to respond with header
time.sleep(1)
+
+ #Send a blank message to open the connection
+ stripped_msg = '{"message": {"to": ["' + swarm_id + '"], "payload":}}'
+ size = hex(len(stripped_msg))[2:] + "\r\n"
+ chunk = stripped_msg + "\r\n"
+ conn.send(size+chunk)
+
+ #Execute further messages
if wrap == False:
while True:
try:
msg = sys.stdin.readline()
- if (len(msg) < 1):
+ if msg == "\n":
+ stripped_msg = '{"message": {"to": ["' + swarm_id + '"], "payload":}}'
+ elif (len(msg) < 1):
break
- stripped_msg = msg.strip()
+ else:
+ stripped_msg = msg.strip()
size = hex(len(stripped_msg))[2:] + "\r\n"
chunk = stripped_msg + "\r\n"
- conn.send(size+chunk)
+ print size+chunk
+ conn.send(size+chunk)
except Exception as e:
print "some sort of problem", e
else:
@@ -49,7 +61,8 @@ def produce(hostname, api_key, swarm_id, resource_id, wrap):
payload = sys.stdin.readline()
if (len(payload) < 1):
break
- msg = '{"message": {"to": ["' + swarm_id + '"], "payload": ' + payload + '}}'
+ stripped_payload = payload.strip()
+ msg = '{"message": {"to": ["' + swarm_id + '"], "payload": ' + stripped_payload + '}}'
size = hex(len(msg))[2:] + "\r\n"
chunk = msg + "\r\n"
conn.send(size+chunk)

0 comments on commit ab42f17

Please sign in to comment.