def send_req_wired(host, port): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" "%s:%d" % ((host), port)) print("sending req to wired_sdn") sw = orc_to_wired.orc_to_wired() ob = sw.switch.addflow.flow.add("5674") ob.ip_src = "192.168.1.2" ob.ip_dst = "192.168.1.3" lm = sw.switch.deleteflow.flow.add("5644") lm.ip_src = "192.168.1.5" lm.ip_dst = "192.168.1.6" kd = sw.switch.changeflow.flow.add("5684") kd.ip_src_old = "192.168.1.98" kd.ip_dst_old = "192.168.1.45" kd.ip_src_new = "192.168.1.67" kd.ip_dst_new = "192.168.1.24" kd.port_new = '3453' a = sw.get() # this default should return a serializable version of obj or raise TypeError packet = (json.dumps(a, default=lambda x: x.__dict_)) print(packet) # print(type(packet)) socket.send_json(packet) msg = socket.recv(1024) print("received reply: %s" % msg)
def getSongBytes(server): try: socket = ctx.socket(zmq.REQ) ip = server["ip"] port = server["port"] socket.connect("tcp://" + ip + ":" + str(port)) filenames = server["files"] files = {} for f in filenames: print("apending bytes") socket.send_json({ "origin": "client", "type": "download", "data": { "filename": f } }) ans = socket.recv_json() if ans["status"] == "ok": files[f] = ans["data"]["bytes"].encode('iso8859-15') else: print("Error, ", ans) return files except Exception as e: print("Ocurrió un error al obtener la parte de la canción, ", e)
def send_array(socket, A, flags=0, copy=True, track=False): """send a numpy array with metadata""" md = dict( dtype = str(A.dtype), shape = A.shape, ) socket.send_json(md, flags|zmq.SNDMORE) socket.send(A, flags, copy=copy, track=track)
def send_image(socket, im, flags=0, copy=False, track=False): """send a PIL image with metadata""" md = dict( size = im.size, mode = im.mode, ) socket.send_json(md, flags|zmq.SNDMORE) return socket.send(im.tostring(), flags, copy=copy, track=track)
def send_array(socket, A, f_num, flags=0, copy=True, track=False): """send a numpy array with metadata""" md = dict( dtype='uint8', shape=A.shape, frame_num=f_num, ) socket.send_json(md, flags | zmq.SNDMORE) print("sending from collector 1") return socket.send(A, flags, copy=copy, track=track)
def send_array(socket, nparray, flags=0, copy=True, track=False): """send a numpy array with metadata from http://pyzmq.readthedocs.io/en/latest/serialization.html """ md = dict( dtype=str(nparray.dtype), shape=nparray.shape, ) socket.send_json(md, flags | zmq.SNDMORE) return socket.send(nparray, flags, copy=copy, track=track)
def send_array(socket, A, flags=0, copy=True, track=False): md = dict( dtype=str(A.dtype), shape=A.shape, ) # Made a dictionary of dtype of array and shape of array so that at server side we know # at the time of transforming array from buffer we must know the shape to get it back. socket.send_json(md, flags | zmq.SNDMORE) #Sent the image using json to the server return socket.send(A, flags, copy=copy, track=track)
def _getIPCMsg(self, flag): context = zmq.Context() socket = context.socket(zmq.REQ) socket.setsockopt(zmq.LINGER, 0) #socket.connect("ipc://"+baseParams['IPCFile']) socket.connect(baseParams['tcpaddr']) socket.send_json(flag) poller = zmq.Poller() poller.register(socket, zmq.POLLIN) if poller.poll(360 * 1000): # 10s timeout in milliseconds return (socket.recv_json()) else: return ({'data': 'The process has no response'})
def keepalive(socket=None, subscriber=None): while True: msg = { "action": "announcement", "state": subscriber.state, "id": subscriber.id, "file": subscriber.file, "cfg": subscriber.cfg } socket.send_json(msg) msg = socket.recv_json() subscriber.cfg = msg["cfg"] time.sleep(10)
def join_cluster(self, cluster_cmd_uri): socket = self.ctx.socket(zmq.REQ) socket.connect('tcp://{}'.format(cluster_cmd_uri)) socket.send_json({ 'type': 'JOIN', 'node': self.local_node.details, 'timestamp': time.time() }) resp = socket.recv_json() for node in resp['nodes']: self.add_node(node) socket.close()
def send_req_wired(host, port): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" "%s:%d" % ((host), port)) print("sending req to wired_sdn") sw = orc_to_wired.orc_to_wired() a = sw.get() # this default should return a serializable version of obj or raise TypeError packet = (json.dumps(a, default=lambda x: x.__dict_)) print(packet) # print(type(packet)) socket.send_json(packet) msg = socket.recv(1024) print("received reply: %s" % msg)
def _handle_message(self, message, args, socket, build_index): """ Receives messages from the controller and calls the appropriate function. :param message: The message dict from controller. :param args: The args defined upon initializing this script defining binary. :param socket: The zmq socket from controller. :param build_index: The dict containing all active binaries. """ if message["type"] == "start_build": build_info = self._start_binary(message, args) build_port = build_info["build_port"] build_index[build_port] = build_info socket.send_json(build_info) elif message["type"] == "keep_alive": build_port = message["build_port"] build_index[build_port]["last_keep_alive"] = time.time() socket.send_json(message) elif message["type"] == "kill_build": build_port = message["build_port"] pid = build_index[build_port]["build_pid"] os.kill(pid, signal.SIGKILL) socket.send_json({"type": "build_killed", "build_pid": pid}) print("Killing build with pid: {} on port: {}".format( pid, build_port)) else: socket.send_json({"type": "no_response"})
def crack(f_name=None, e=None, subscriber=None, socket=None): print("Começando quebra do arquivo ", f_name) # executa o john cmd = "" if len(subscriber.cfg): cmd = "exec john -i=" + subscriber.cfg + " " + f_name + " --session=" + subscriber.id else: cmd = "exec john " + f_name + " --session=" + subscriber.id print(cmd) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) while True: if e.isSet(): e.clear() p.kill() subscriber.state = "ocioso" print("Recebido comando para parar.") break if p.poll() is not None: subscriber.state = "ocioso" cmd = "john --show " + f_name p = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) results = p.stdout.decode("utf-8").splitlines() print(results) erro = p.stderr if erro is not None: print(erro.decode("utf-8")) if len(results) >= 2: print("Arquivo quebrado!!") results = results[:len(results) - 2] results = "\n".join(results) else: print("ERRO: Não foi possível quebrar o arquivo.") results = "" msg = { "action": "done", "f_name": f_name, "id": subscriber.id, "status": p.returncode, "results": results, "data": datetime.now().strftime("%d/%m/%Y %H:%M:%S") } socket.send_json(msg) socket.recv_json() break
def _send_kill_build(socket, build_info: dict) -> dict: """ This sends a command to the launch_binaries daemon running on a remote node to terminate a given binary. :param socket: The zmq socket. :param build_info: A diciontary containing the build_port. :return A kill_status indicating build has been succesfully terminated. """ build_port = build_info["build_port"] request = {"type": "kill_build", "build_port": build_port} socket.send_json(request) kill_status = socket.recv_json() return kill_status
def tryConnect(ip=getLocalIp(), port=PORT, timeout=10, dt=0.01): ok = None context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" + str(ip) + ":" + str(port)) socket.send_json({"action": "list"}) # print "send at ", time.time() t = time.time() while t + timeout > time.time(): try: socket.recv_json(zmq.DONTWAIT) ok = (ip, port) except Exception, e: time.sleep(dt) continue
def send_ack_master(): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://%s:%s" % (MASTER_IP, master_ACK_port)) header_data = { "machine": machine_name, ##ID instead don't forget to modify "username": "******", "filename": "vid_1.mp4", "numberofchunks": 15 } header_data_sent_to_master = json.dumps(header_data) socket.send_json(header_data_sent_to_master) replica_list = socket.recv_json() print(replica_list) socket.close() return replica_list
def _send_keep_alive(socket, build_info: dict) -> dict: """ This sends a command to the launch_binaries daemon running on a remote node to mark a given binary as still alive, preventing garbage collection. :param socket: The zmq socket. :param build_info: A diciontary containing the build_port. :return a heartbeat indicating build is still alive. """ build_port = build_info["build_port"] request = {"type": "keep_alive", "build_port": build_port} socket.send_json(request) heartbeat = socket.recv_json() return heartbeat
def _send_start_build(socket, controller_address: str) -> dict: """ This sends a command to the launch_binaries daemon running on a remote node to start a binary connected to the given controller address. :param socket: The zmq socket. :param controller_address: The host name or ip address of node running the controller. :return Build info dictionary containing build port. """ request = { "type": "start_build", "controller_address": controller_address } socket.send_json(request) build_info = socket.recv_json() return build_info
def send_req_wireless(host, port): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" "%s:%d" % ((host), port)) print("sending req to wireless sdn") sw = orc_to_wireless.orc_to_wireless() ob = sw.handover.client.add('01:22:45:67:89:ac') ob.old_mac = '01:32:35:67:90:ab' ob.new_mac = '01:12:45:67:89:ab' a = sw.get() # this default should return a serializable version of obj or raise TypeError packet = (json.dumps(a, default=lambda x: x.__dict_)) print(packet) # print(type(packet)) socket.send_json(packet) msg = socket.recv(1024) print("received reply: %s" % msg)
def update_ctr(self, ctr_ip): try: LOG.info("Controller IP Address= {0}".format(ctr_ip)) context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://{0}:50165".format(ctr_ip)) ip = str(subprocess.check_output('hostname -I', shell=True)) nc = len(ip) - 3 ip = ip[2:nc] host_ip = ip.strip() LOG.info("HOST IP Address= {0}".format(host_ip)) mapping = {"VLAN": "10", "IP": host_ip, "MAC": "00:50:56:b8:11:c9"} socket.send_json(mapping) LOG.info("Received " + socket.recv_json()["reply"] + " event from SDN controller.") except BaseException as err: LOG.error("Passing IP to SDN controller failed! ERROR: {0}".format( str(err)))
def ping(seed, q, time, log): """ Process that make ping to a seed. """ context = zmq.Context() socket = noBlockREQ(context, timeout=time) socket.connect(f"tcp://{seed[0]}:{seed[1]}") status = True log.debug(f"PING to {seed[0]}:{seed[1]}", "Ping") try: socket.send_json(("PING", )) msg = socket.recv_json() log.info(f"Received {msg} from {seed[0]}:{seed[1]} after ping", "Ping") except zmq.error.Again as e: log.debug(f"PING failed -- {e}", "Ping") status = False q.put(status)
def tryConnect(ip=getLocalIp(), port=PORT, timeout=4, dt=0.01): ok = None context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" + str(ip) + ":" + str(port)) socket.send_json({"action": "list"}) # print "send at ", time.time() t = time.time() while t + timeout > time.time(): try: socket.recv_json(zmq.DONTWAIT) ok = (ip, port) except Exception as e: time.sleep(dt) continue socket.setsockopt(zmq.LINGER, 0) socket.close() context.term() return ok
def main(): sim = Simulation(SpeedLimit=0.5, RandomChanges=10) # instantiate the simulation wrapper # Setup comm sockets context = zmq.Context() socket = context.socket(zmq.PUB) # publish to all upstreams services socket.bind("tcp://127.0.0.1:5550") # localhost only, no encryption # Main Loop while True: # -------------------------------------------------------------------------- # Calculate next round # -------------------------------------------------------------------------- sim.Solve() # TODO destabilization code - restore? # -------------------------------------------------------------------------- # Send state to visualization/freq model # -------------------------------------------------------------------------- socket.send_json(sim.Output())
def send_manage_message(endpoint: str, action: dict, timeout: int = 5): """ Sends a 'management' message, following the threatbus-zmq-app protocol to either subscribe or unsubscribe this application to/from Threat Bus. @param endpoint A host:port string to connect to via ZeroMQ @param action The message to send as JSON @param timeout The period after which the connection attempt is aborted """ context = zmq.Context() socket = context.socket(zmq.REQ) socket.setsockopt(zmq.LINGER, 0) socket.connect(f"tcp://{endpoint}") socket.send_json(action) poller = zmq.Poller() poller.register(socket, zmq.POLLIN) reply = None if poller.poll(timeout * 1000): reply = socket.recv_json() socket.close() context.term() return reply
def post(self, experiment_id, user_id): if self.get_cookie('sessionid') not in self.db['user']: self.set_status(500) self.finish() return client_query = {} client_query['QUERY'] = self.get_argument('QUERY') client_query['CLIENT_ID'] = self.get_cookie('sessionid') client_query['CLIENT_RESPONSE'] = {} client_query['CLIENT_RESPONSE']['WORKSITE'] = self.get_argument('CLIENT_RESPONSE_WORKSITE', None) client_query['CLIENT_RESPONSE']['CONCLUSION'] = self.get_argument('CLIENT_RESPONSE_CONCLUSION', None) client_query['QUERY_INDEX'] = int(self.get_argument('QUERY_INDEX', -1)) client_query['QUERY_STATUS'] = self.get_argument('QUERY_STATUS', '') context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect('ipc://%s'%self.totem) socket.send_json(client_query) server_response = socket.recv_json() socket.close() # return self.write(json.dumps(server_response))
def run(self): self.isStopped = False print "Starting nameserver on", getLocalIp(), str(PORT) time.sleep(1) self.canGo = True context = zmq.Context() socket = context.socket(zmq.REP) # socket.bind("tcp://" + config.local_ip + ":" + str(config.name_port)) socket.bind("tcp://*:" + str(PORT)) programs = {} counter = 0 import SimpleHTTPServer, SocketServer WPORT = 8080 import pprint, StringIO class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write("<html><body>"); self.wfile.write("<h1>Programs</h1><pre>"); s = StringIO.StringIO() pprint.pprint(programs, s) self.wfile.write(s.getvalue()) self.wfile.write("</pre>"); self.wfile.write("</body></html>"); self.wfile.close(); Handler = MyHandler # self.httpd = SocketServer.ThreadingTCPServer(("", WPORT), Handler) print "serving WWW at port", WPORT # self.www = threading.Thread(target=self.httpd.serve_forever).start() while self.canGo: data = {} try: data = socket.recv_json(zmq.DONTWAIT) print data except Exception, e: time.sleep(0.01) continue print 'NS recive at', time.time() try: data['action'] except: data['action'] = 'ping' try: if data['action'] == 'register': try: data['location'] except: data['location'] = 'local' programs[data['name']] = dict(time=time.time(), ip=data['ip'], port=data['port'], location=data['location']) print "registrating ", data['name'], programs[data['name']] socket.send_json(dict(status='ok')) continue if data['action'] == 'list': d = [] for k, v in programs.iteritems(): d.append(dict(name=k, data=v)) socket.send_json(d) print 'send at', time.time() continue if data['action'] == 'ping': socket.send_json(dict(status='ok')) continue except Exception, e: print e socket.send_json(dict(status='fail', text=str(e))) continue
def test_resultsServer(): """ tests pyneal.src.resultsServer """ # create settings dictionary settings = {'pynealScannerPort': port, 'pynealHost': host, 'numTimepts': 3, 'launchDashboard': False, 'seriesOutputDir': paths['testDataDir']} scanReceiver = ScanReceiver(settings) scanReceiver.daemon = True scanReceiver.start() # Set up Pyneal Scanner simulator for making a connection to the scanReceiver context = zmq.Context.instance() socket = context.socket(zmq.PAIR) socket.connect('tcp://{}:{}'.format(host, port)) while True: msg = 'hello from test pyneal scanner simulator' socket.send_string(msg) resp = socket.recv_string() if resp == msg: break # Send data to scan receiver ds = nib.load(join(paths['testDataDir'], 'testSeries.nii.gz')) ds_array = ds.get_data() ds_affine = ds.affine for volIdx in range(ds_array.shape[3]): # grab this volume from the dataset thisVol = np.ascontiguousarray(ds_array[:, :, :, volIdx]) # build header volHeader = {'volIdx': volIdx, 'dtype': str(thisVol.dtype), 'shape': thisVol.shape, 'affine': json.dumps(ds_affine.tolist()), 'TR': str(1000)} # send header as json socket.send_json(volHeader, zmq.SNDMORE) # now send the voxel array for this volume socket.send(thisVol, flags=0, copy=False, track=False) # list for response socketResponse = socket.recv_string() # test scanReceiver get functions np.testing.assert_equal(scanReceiver.get_affine(), ds_affine) np.testing.assert_equal(scanReceiver.get_slice(1,10), ds_array[:, :, 10, 1]) np.testing.assert_equal(scanReceiver.get_vol(2), ds_array[:, :, :, 2]) # test saving (then delete) scanReceiver.saveResults() os.remove(join(paths['testDataDir'], 'receivedFunc.nii.gz')) # assuming nothing crashed, shutdown scanReceiver server scanReceiver.killServer()
import socket import zmq import sys import time import os context = zmq.Context() socket = context.socket(zmq.REP) socket.bind("tcp://localhost:1337") while True: filename = socket.recv() if os.path.isfile(filename): socket.send_json({'result' : 'success', 'content' : open(filename).read()}) else: socket.send_json({'result' : 'fail', 'error' : 'not found') print("done")
def ack(addr, socket_type=zmq.PAIR, **additional_info): socket = ctx.socket(socket_type) socket.connect(addr) socket.send_json(msg(act='ack', **additional_info)) socket.close()
def _ipcFun(self, socket): message = socket.recv_json() if message['action'] == 'status': socket.send_json({ 'data': { "server response! PID:" + str(os.getpid()): systemDict } }) elif message['action'] == 'stop': self.flag = False systemDict['main']['target'] = 'off' self._checkProcessEnd() stdLogger.info('check Process completion.') socket.send_json({'data': 'Process stop completion.'}) systemDict['main']['state'] = 'off' elif message['action'] == 'forcestop': self.flag = False socket.send_json({'data': 'Process forcestop completion.'}) #time.sleep(5) systemDict['main']['state'] = 'off' elif message['action'] == 'start': socket.send_json( {'data': "Start to finish,pid:" + str(os.getpid())}) elif message['action'] == 'addlog': stdLogger.info(message['data']) socket.send_json({'data': 'addlog completion.'}) else: socket.send_json({'data': 'unknow parameter'})
userlist = list(set(userlist)) loggedinusers = len(userlist) WHO.close() # NEW: now sending a dictionary of the statistics senddict = {'version':labstatsversion, 'timestamp':timestmp, 'hostname':localhost, 'os':os, 'model':model, 'totalmem':totalmem, 'totalcommit':totalcommit, 'totalcpus':totalcpus, 'usedmem':usedmem, 'committedmem':committedmem, 'pagefaultspersec':pagefaultspersec, 'cpupercent':cpupercent, 'cpuload':cpuload, 'loggedinusers':loggedinusers, 'loggedinuserbool':loggedinuserbool} socket.send_json(senddict) response = socket.recv() print "Recieved reply: %s" % response if debug: print "Labstats Version: ", labstatsversion print "Time: ", timestmp print "Hostname: ", localhost print "OS: ", os print "Model: ", model print "Total Memory: ", totalmem print "Total Committed Memory: ", totalcommit print "Total CPUs: ", totalcpus print "Used Memory: ", usedmem print "Committed memory: ", committedmem print "Page Faults per second: ", pagefaultspersec