Skip to content

Instantly share code, notes, and snippets.

@jameskyle
Created April 10, 2014 18:32
Show Gist options
  • Select an option

  • Save jameskyle/10409855 to your computer and use it in GitHub Desktop.

Select an option

Save jameskyle/10409855 to your computer and use it in GitHub Desktop.
def read_bytes(length):
with open('/dev/urandom') as data:
data = os.read(data.fileno(), length)
return data
def send(conn, data):
sent = 0
while sent < len(data):
SEMAPHORE.acquire()
sent += conn.send(data[sent:])
SEMAPHORE.release()
def client(conn, flow):
gevent.sleep()
data = read_bytes(flow.num_bytes)
send(conn, data)
def spawn_flows(flowdist):
LOG.debug("Spawn Flows called with {0}".format(flowdist))
pool = Pool(10)
# TODO: Support UDP socket.SOCK_DGRAM flows
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#destination = config.NODE_MAP[flowdist.source.destination]['traffic']
destination = '127.0.0.1'
conn.connect((destination, config.STREAM_PORT))
flows = list(flowdist.simulation())
total = sum([flow.num_bytes for flow in flows])
LOG.debug("Sending simulation of length {0}".format(total / 1024 / 1024))
# TODO: check your sendall returns
conn.sendall(struct.pack('!Q', total))
#start = time.time()
[pool.spawn(
client,
conn,
flow) for flow in flows]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment