Exemplo n.º 1
1
 def listen_push_port(self, port):
   port.sockets = []
   for port_url in port.port_urls:
     socket = self.context.socket(zmq.PUSH)
     socket.setsockopt(zmq.HWM, self.queue_size)
     socket.connect(port_url)
     port.sockets.append(socket)
Exemplo n.º 2
1
def brute_zmq(host, port=5555, user=None, password=None, db=0):

    context = zmq.Context()

    # Configure
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, b"")  # All topics
    socket.setsockopt(zmq.LINGER, 0)  # All topics
    socket.RCVTIMEO = 1000  # timeout: 1 sec

    # Connect
    socket.connect("tcp://%s:%s" % (host, port))

    # Try to receive
    try:
        socket.recv()

        return True
    except Exception:
        return False
    finally:
        socket.close()
def zmq_recv(context,url):

    socket = context.socket(zmq.SUB)
    # socket = context.socket(zmq.REP)
    socket.connect(url)
    socket.setsockopt(zmq.SUBSCRIBE,''.encode('utf-8'))  # 接收所有消息

    zhanbao=0
    buzhanbao=0
    start_time = time.clock()
    while True:
        b = socket.recv();
        # socket.send(b'1')
        # print(b)

        end_time = time.clock()
        if len(b)==1:
            # print('总计耗时',end_time-start_time)
            break

        size = len(b)
        # print(size)

        # if end_time-start_time > 10:
        #     pass
        #     break
        if size>10:
            zhanbao = zhanbao + 1

        else:
            buzhanbao = buzhanbao + 1

    print('接收不粘包',buzhanbao)
    print('接收粘包',zhanbao)
Exemplo n.º 4
0
	def publishPoint(self, point):
		streams = (	"%s.%s" % (self.prefix, point["pointname"]),
				"%s.%s.multipart" % (self.prefix, point["pointname"]),
				"%s.allpoints" % (self.prefix),
				"%s.allpoints.multipart" % (self.prefix))
		streams = [str(i) for i in streams]
		for stream in streams:
			if stream not in self.streams:
				socket = zmq.Context().socket(zmq.PUB)
				socket.setsockopt(HIGHWATER, 15000)
				port = socket.bind_to_random_port("tcp://%s" % self.ip)
				self.streams[stream] = socket
				if "multipart" in stream:
					try:
						self.ns.publishService(stream, "tcp://%s:%s" % (self.ip, port), self.publishTimeout, "pub/sub", "ZmqMultipartPoint")
					except nnslib.NameServerException, e:
						if str(e) == "ZmqMultipartPoint is an unknown data type":
							self.ns.addDataType("ZmqMultipartPoint", "ZmqMultipart", "host,time,pointname,val,unit", "")
						else:
							raise
				else:
					try:
						self.ns.publishService(stream, "tcp://%s:%s" % (self.ip, port), self.publishTimeout, "pub/sub", "Point")
					except nnslib.NameServerException, e:
						if str(e) == "Point is an unknown data type":
							self.ns.addDataType("Point", "JSON", "host,time,pointname,val,unit", "")
						else:
							raise
Exemplo n.º 5
0
    async def run(self):
        socket = self.context.socket(zmq.DEALER)
        socket.setsockopt(zmq.IDENTITY, self.identity())
        socket.curve_secretkey = self.secret_key
        socket.curve_publickey = self.public_key
        socket.curve_serverkey = self.server_key
        socket.connect(f'tcp://{self.server["addr"]}:{self.server["port"]}')

        asyncio.create_task(self.heartbeat(socket))

        while True:
            msg = await socket.recv_multipart()
            respond = msg[1:]
            cmd = respond[0]
            if cmd == RELAY:
                client_addr = respond[1]
                if client_addr not in self.sessions:
                    try:
                        reader, writer = await asyncio.open_connection(
                            host=self.service["addr"],
                            port=self.service["port"])
                    except Exception as e:
                        logging.error(f"Can't connect to server: {e}")
                        continue
                    else:
                        logging.info(
                            f'connected to {writer.get_extra_info("peername")}'
                        )
                        self.sessions[client_addr] = reader, writer
                        asyncio.create_task(
                            self.from_service(client_addr, socket))
                asyncio.create_task(self.to_service(client_addr, respond[2]))
            elif cmd == EXCEPTION:
                logging.error(respond[1].decode())
Exemplo n.º 6
0
    def _set_tcp_keepalive(self, socket):
        """ Set a series of TCP keepalive options on the socket if
        and only if
          1) they are specified explicitly in the config and
          2) the version of pyzmq has been compiled with support

        We ran into a problem in FedoraInfrastructure where long-standing
        connections between some hosts would suddenly drop off the
        map silently.  Because PUB/SUB sockets don't communicate
        regularly, nothing in the TCP stack would automatically try and
        fix the connection.  With TCP_KEEPALIVE options (introduced in
        libzmq 3.2 and pyzmq 2.2.0.1) hopefully that will be fixed.

        See the following
          - http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
          - http://api.zeromq.org/3-2:zmq-setsockopt
        """

        keepalive_options = {
            # Map fedmsg config keys to zeromq socket constants
            'zmq_tcp_keepalive': 'TCP_KEEPALIVE',
            'zmq_tcp_keepalive_cnt': 'TCP_KEEPALIVE_CNT',
            'zmq_tcp_keepalive_idle': 'TCP_KEEPALIVE_IDLE',
            'zmq_tcp_keepalive_intvl': 'TCP_KEEPALIVE_INTVL',
        }
        for key, const in keepalive_options.items():
            if key in self.c:
                attr = getattr(zmq, const, None)
                if not attr:
                    self.log.warn("zmq.%s not available" % const)
                else:
                    self.log.debug("Setting %r %r" % (const, self.c[key]))
                    socket.setsockopt(attr, self.c[key])
Exemplo n.º 7
0
    def send(self, data):

        try:
            addrinfo = socket.getaddrinfo(self.host, None)[0]
            self.socket = socket.socket(addrinfo[0], socket.SOCK_DGRAM)

            # Set Time-to-live (optional)
            if self.timeout:
                ttl_bin = struct.pack('@i', self.timeout)
            else:
                ttl_bin = struct.pack(
                    '@i', 0
                )  # self.timout is None if not set. TODO: This is unintuitive behaviour

            if addrinfo[0] == socket.AF_INET:  # IPv4
                self.socket.setsockopt(socket.IPPROTO_IP,
                                       socket.IP_MULTICAST_TTL, ttl_bin)
                sock_type = socket.IPPROTO_IP
            else:  # IPv6
                socket.setsockopt(socket.IPPROTO_IPV6,
                                  socket.IPV6_MULTICAST_HOPS, ttl_bin)
                sock_type = socket.IPPROTO_IPV6

            # Ignore packets sent from self TODO: make this an option
            self.socket.setsockopt(sock_type, socket.IP_MULTICAST_LOOP, 0)
            self.socket.sendto(pickle.dumps(data), (addrinfo[4][0], self.port))

        except socket.error as e:
            raise socket.error('{}{}: {}{}'.format(settings.RED,
                                                   self.identifier, e,
                                                   settings.NORMAL))
        finally:
            if self.socket:
                self.socket.close()
Exemplo n.º 8
0
def getMessage(service=None, timeout=1000):
    if service is None or service not in service_list:
        raise Exception("invalid service")
    socket = messaging.sub_sock(service_list[service].port)
    socket.setsockopt(zmq.RCVTIMEO, timeout)
    ret = messaging.recv_one(socket)
    return ret.to_dict()
Exemplo n.º 9
0
async def receive(pub_endpoint: str, topic: str, indicator_queue: asyncio.Queue):
    """
    Starts a zmq subscriber on the given endpoint and listens for new messages
    that are published on the given topic (zmq prefix matching). Depending on
    the topic suffix, Indicators are enqueued to the indicator_queue.
    @param pub_endpoint A host:port string to connect to via zmq
    @param topic The topic prefix to subscribe to intelligence items
    @param indicator_queue The queue to put arriving IoCs into
    """
    global logger
    socket = zmq.Context().socket(zmq.SUB)
    socket.connect(f"tcp://{pub_endpoint}")
    socket.setsockopt(zmq.SUBSCRIBE, topic.encode())
    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)
    logger.info(f"Receiving via ZMQ on topic {pub_endpoint}/{topic}")
    while True:
        socks = dict(
            poller.poll(timeout=100)
        )  # note that smaller timeouts may increase CPU load
        if socket in socks and socks[socket] == zmq.POLLIN:
            try:
                topic, msg = socket.recv().decode().split(" ", 1)
            except Exception as e:
                logger.error(f"Error decoding message: {e}")
                continue
            # the topic is suffixed with the message type
            if not topic.endswith("indicator"):
                # pyvast-threatbus is not (yet) interested in Sightings or SnapshotRequests
                logger.debug(f"Skipping unsupported message: {msg}")
                continue
            await indicator_queue.put(msg)
        else:
            await asyncio.sleep(0.05)  # free event loop for other tasks
Exemplo n.º 10
0
def metadata(meta_ifinfo, ifname, expconfig):
    """Seperate process that attach to the ZeroMQ socket as a subscriber.
	
	Will listen forever to messages with topic defined in topic and update
	the meta_ifinfo dictionary (a Manager dict).
	"""
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.connect(expconfig['zmqport'])
    socket.setsockopt(zmq.SUBSCRIBE, expconfig['modem_metadata_topic'])
    # End Attach
    while True:
        data = socket.recv()
        try:
            ifinfo = json.loads(data.split(" ", 1)[1])
            if (expconfig["modeminterfacename"] in ifinfo
                    and ifinfo[expconfig["modeminterfacename"]] == ifname):
                # In place manipulation of the reference variable
                for key, value in ifinfo.iteritems():
                    meta_ifinfo[key] = value
        except Exception as e:
            if expconfig['verbosity'] > 0:
                print("Cannot get modem metadata in http container {}"
                      ", {}").format(e, expconfig['guid'])
            pass
Exemplo n.º 11
0
def setsockopt(k, p, args_addr):
    arg_list = [
        ("int", "sockfd"),
        ("int", "level"),
        ("int", "optname"),
        ("const void*", "optval"),
        ("socklen_t", "optlen"),
    ]
    args = k._get_socketcall_args(p, "setsockopt", args_addr, arg_list)

    socket_handle = k.z.handles.get(args.sockfd)
    if socket_handle is None:
        k.logger.error("Invalid socket handle")
        return -1
    socket = socket_handle.socket

    optval = p.memory.read(args.optval, args.optlen)

    try:
        (level, name) = _socktopt_linux_to_python(args.level, args.optname)
        socket.setsockopt(args.level, args.optname, optval)
        return 0
    except Exception as e:
        print("[setsockopt] failed:", e)

    return 0
Exemplo n.º 12
0
    def _set_tcp_keepalive(self, socket):
        """ Set a series of TCP keepalive options on the socket if
        and only if
          1) they are specified explicitly in the config and
          2) the version of pyzmq has been compiled with support

        We ran into a problem in FedoraInfrastructure where long-standing
        connections between some hosts would suddenly drop off the
        map silently.  Because PUB/SUB sockets don't communicate
        regularly, nothing in the TCP stack would automatically try and
        fix the connection.  With TCP_KEEPALIVE options (introduced in
        libzmq 3.2 and pyzmq 2.2.0.1) hopefully that will be fixed.

        See the following
          - http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
          - http://api.zeromq.org/3-2:zmq-setsockopt
        """

        keepalive_options = {
            # Map fedmsg config keys to zeromq socket constants
            'zmq_tcp_keepalive': 'TCP_KEEPALIVE',
            'zmq_tcp_keepalive_cnt': 'TCP_KEEPALIVE_CNT',
            'zmq_tcp_keepalive_idle': 'TCP_KEEPALIVE_IDLE',
            'zmq_tcp_keepalive_intvl': 'TCP_KEEPALIVE_INTVL',
        }
        for key, const in keepalive_options.items():
            if key in self.c:
                attr = getattr(zmq, const, None)
                if attr:
                    self.log.debug("Setting %r %r" % (const, self.c[key]))
                    socket.setsockopt(attr, self.c[key])
Exemplo n.º 13
0
    def _custom_validate(self, verify, trust_bundle):
        """
        Called when we have set custom validation. We do this in two cases:
        first, when cert validation is entirely disabled; and second, when
        using a custom trust DB.
        Raises an SSLError if the connection is not trusted.
        """
        # If we disabled cert validation, just say: cool.
        if not verify:
            return

        successes = (
            SecurityConst.kSecTrustResultUnspecified,
            SecurityConst.kSecTrustResultProceed,
        )
        try:
            trust_result = _evaluate_trust(trust_bundle)
            if trust_result in successes:
                return
            reason = "error code: %d" % (trust_result, )
        except Exception as e:
            # Do not trust on error
            reason = "exception: %r" % (e, )

        # SecureTransport does not send an alert nor shuts down the connection.
        rec = _build_tls_unknown_ca_alert(version())
        socket.sendall(rec)
        # close the connection immediately
        # l_onoff = 1, activate linger
        # l_linger = 0, linger for 0 seoncds
        opts = struct.pack("ii", 1, 0)
        socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, opts)
        close()
        raise ssl.SSLError("certificate verify failed, %s" % reason)
Exemplo n.º 14
0
def get_unused_port() -> int:
    import socket
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # just in case
    s.bind(('', 0))
    port = s.getsockname()[1]
    s.close()
    return port
Exemplo n.º 15
0
 def create_zmq_socket(self):
     try:
         socket = self.ctx.socket(zmq.REQ)
         socket.setsockopt(zmq.LINGER, 0)
         return socket
     except Exception as e:
         self.log.error('Cannot create socket %s' % e)
         raise
Exemplo n.º 16
0
    def __init__(self, port, mgroup, reuse=True, ttl=20):
        '''
    Creates a socket, bind it to a given port and join to a given multicast group.
    IPv4 and IPv6 are supported.
    @param port: the port to bind the socket
    @type port: int
    @param mgroup: the multicast group to join
    @type mgroup: str
    @param reuse: allows the reusing of the port
    @type reuse: boolean (Default: True)
    @param ttl: time to leave
    @type ttl: int (Default: 20)
    '''
        # get info about the IP version (4 or 6)
        addrinfo = socket.getaddrinfo(mgroup, None)[0]
        socket.socket.__init__(self, addrinfo[0], socket.SOCK_DGRAM,
                               socket.IPPROTO_UDP)

        # Allow multiple copies of this program on one machine
        if (reuse):
            self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            if hasattr(socket, "SO_REUSEPORT"):
                self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        # Bind to the port
        self.bind(('', port))
        # Set Time-to-live (optional) and loop count
        ttl_bin = struct.pack('@i', ttl)
        if addrinfo[0] == socket.AF_INET:  # IPv4
            self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL,
                            ttl_bin)
            self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
        else:  # IPv6
            self.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS,
                            ttl_bin)
            socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP,
                              1)

        #join to the multicast group
        group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0])
        try:
            if addrinfo[0] == socket.AF_INET:  # IPv4
                mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
                self.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
                                mreq)
            else:  #IPv6
                mreq = group_bin + struct.pack('@I', 0)
                self.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP,
                                mreq)
        except socket.error, (errn, msg):
            err = str(msg)
            if errn in [19]:
                err = ''.join([
                    "socket.error[",
                    str(errn), "]: ", msg,
                    ",\nis multicast route set? e.g. sudo route add -net 224.0.0.0 netmask 224.0.0.0 eth0"
                ])
            raise Exception(err)
Exemplo n.º 17
0
 def create_socket(socktype, endpoints,flag):
     socket = zmq.Socket(zmq.Context.instance(), socktype)
     socket.setsockopt(zmq.LINGER, 0)
     for endpoint in endpoints:
         if flag==1:
             socket.bind(endpoint)
         else:
             socket.connect(endpoint)
     return socket
Exemplo n.º 18
0
def send(filename):
    yy = atpic.log.setname(xx, 'send')
    atpic.log.debug(yy, 'input:', filename)
    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.setsockopt(zmq.HWM, 8)
    socket.connect("tcp://127.0.0.1:5000")
    socket.send(filename)
    socket.close()
Exemplo n.º 19
0
 def run(self):
     context = zmq.Context()
     socket = context.socket(zmq.SUB)
     try:
         socket.connect(self.msg_svr)
         socket.setsockopt(zmq.SUBSCRIBE, "ses_vc_switch")
     except Exception, e:
         self.logger.warning("Can not connect to msg_svr %s %s",
                             self.msg_svr, e)
Exemplo n.º 20
0
    def create_zmq_socket(self):
        self.log.info("Creating Socket")

        try:
            socket = self.ctx.socket(zmq.REQ)
            socket.setsockopt(zmq.LINGER, 0)
            return socket
        except Exception as e:
            self.log.error("Cannot create socket %s" % e)
            raise
Exemplo n.º 21
0
    def add_subscription(self, node):
        sub_uri = 'tcp://{}'.format(node['event'])
        if sub_uri in self.sockets:
            return

        socket = self.ctx.socket(zmq.SUB)
        socket.connect(sub_uri)
        socket.setsockopt(zmq.SUBSCRIBE, '')
        self.poller.register(socket, zmq.POLLIN)
        self.sockets[sub_uri] = socket
Exemplo n.º 22
0
def bindsocket():
   socket.bind(IPAddress(MY_IP, PORTA))
   socket.setsockopt(socket.D_GRAM, cannoname.header(True))
   from bytrue[] import Byte *
   from byout[] import Byte * 
   Byte = [{1, 0, 0, 0,}, {1, 0, 0, 0,}]    
   socket.ioctl(socket.ioctl_recv, bytrue, byout)
   socket.setblocking(False)
   if BYTE_DATA(socket.recv_into):
   	  socket.socket(NetworkToHostOrder(BYTE_DATA, 0, BYTE_DATA.lenght, flags="None", AsyncCallBack(), not))
   else:
      print("[!!] Skype Grabber sera reiniciado!", sys.argv[3], time.sleep(3), sys.exit(1))
Exemplo n.º 23
0
def servidor(port):
    SERVER_PORT = port
    # inicializar a lista / conjunto de todos os soquetes do cliente conectado
    client_sockets = set()
    # create a TCP socket
    socket = socket.socket()
    # tornar a porta uma porta reutilizável
    socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # ligar o socket ao endereço que especificamos
    socket.bind((SERVER_HOST, SERVER_PORT))
    # escute as próximas conexões
    socket.listen(5)
    print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")

    def listen_for_client(cs):
        """
        Esta função continua ouvindo uma mensagem do socket `cs`
        Sempre que uma mensagem for recebida, transmita-a para todos os outros clientes conectados
        """
        while True:
            try:
                # continue ouvindo por uma mensagem do socket `cs`
                msg = cs.recv(1024).decode()
            except Exception as e:
                # cliente não está mais conectado
                # remova-o do conjunto
                print(f"[!] Error: {e}")
                client_sockets.remove(cs)
            else:
                # se recebemos uma mensagem, substitua o <SEP>
                msg = msg.replace(SEPARAR_TOKEN, ": ")
            # iterar sobre todos os sockets conectados
            for client_socket in client_sockets:
                # e envia a msg
                client_socket.send(msg.encode())

    while True:
        # continua atentos a novas conexões o tempo todo
        client_socket, client_address = socket.accept()
        print(f"[+] {client_address} connected.")
        # adicione o novo cliente conectado aos sockets conectados
        client_sockets.add(client_socket)
        # inicie um novo tópico que ouça as mensagens de cada cliente
        thread = Thread(target=listen_for_client, args=(client_socket,))
        # faz o thread daemon para que ele termine sempre que o thread principal terminar
        thread.daemon = True
        # inicia a thread
        thread.start()
    # fecha client sockets
    for cs in client_sockets:
        cs.close()
    # fecha servidor socket
    socket.close()
Exemplo n.º 24
0
 def __init__(self, port, mgroup, reuse=True, ttl=20):
   '''
   Creates a socket, bind it to a given port and join to a given multicast group.
   IPv4 and IPv6 are supported.
   @param port: the port to bind the socket
   @type port: int
   @param mgroup: the multicast group to join
   @type mgroup: str
   @param reuse: allows the reusing of the port
   @type reuse: boolean (Default: True)
   @param ttl: time to leave
   @type ttl: int (Default: 20)
   '''
   # get info about the IP version (4 or 6)
   addrinfo = socket.getaddrinfo(mgroup, None)[0]
   socket.socket.__init__(self, addrinfo[0], socket.SOCK_DGRAM, socket.IPPROTO_UDP)
   
   # Allow multiple copies of this program on one machine
   if(reuse):
     self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     if hasattr(socket, "SO_REUSEPORT"):
       self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
   # Bind to the port
   self.bind(('', port))
   # Set Time-to-live (optional) and loop count
   ttl_bin = struct.pack('@i', ttl)
   if addrinfo[0] == socket.AF_INET: # IPv4
     self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
     self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
   else:# IPv6
     self.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)
     socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
   interfaces = McastSocket.localifs()
   #join to the multicast group 
   group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0])
   #addr_bin = socket.inet_pton(socket.AF_INET, '192.168.13.3')
   try:
     if addrinfo[0] == socket.AF_INET: # IPv4
       ##mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
       #mreq = group_bin + struct.pack('=I', addr_bin)
       #mreq = group_bin + addr_bin
       for iface in McastSocket.localifs():
         addr_bin = socket.inet_pton(socket.AF_INET, iface[1])
         mreq = group_bin + addr_bin
         self.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
     else: #IPv6
       mreq = group_bin + struct.pack('@I', 0)
       self.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
   except socket.error, (errn, msg):
     if errn in [19]:
       print "socket.error[", errn, "]:", msg, "\nis multicast route set? e.g. sudo route add -net 224.0.0.0 netmask 224.0.0.0 eth0"
       print "\n\n"
     raise
Exemplo n.º 25
0
def get_learner(
    url: str, log_fname: str, job_id: str, job_name: str
) -> tuple[BaseLearner, str | list[str]]:
    """Get a learner from the database running at `url` and this learner's
    process will be logged in `log_fname` and running under `job_id`.

    Parameters
    ----------
    url : str
        The url of the database manager running via
        (`adaptive_scheduler.server_support.manage_database`).
    log_fname : str
        The filename of the log-file. Should be passed in the job-script.
    job_id : str
        The job_id of the process the job. Should be passed in the job-script.
    job_name : str
        The name of the job. Should be passed in the job-script.

    Returns
    -------
    learner : `adaptive.BaseLearner`
        Learner that is chosen.
    fname : str
        The filename of the learner that was chosen.
    """
    _add_log_file_handler(log_fname)
    log.info(
        "trying to get learner", job_id=job_id, log_fname=log_fname, job_name=job_name
    )
    with ctx.socket(zmq.REQ) as socket:
        socket.setsockopt(zmq.LINGER, 0)
        socket.setsockopt(zmq.SNDTIMEO, 300_000)  # timeout after 300s
        socket.connect(url)
        socket.send_serialized(("start", job_id, log_fname, job_name), _serialize)
        log.info("sent start signal, going to wait 60s for a reply.")
        socket.setsockopt(zmq.RCVTIMEO, 300_000)  # timeout after 300s
        reply = socket.recv_serialized(_deserialize)
        log.info("got reply", reply=str(reply))
        if reply is None:
            msg = "No learners to be run."
            exception = RuntimeError(msg)
            log_exception(log, msg, exception)
            raise exception
        elif isinstance(reply, Exception):
            log_exception(log, "got an exception", exception=reply)
            raise reply
        else:
            learner, fname = reply
            log.info("got fname and learner")

    log.info("picked a learner")
    return learner, maybe_lst(fname)
Exemplo n.º 26
0
def create_local_socket(port, pull=False):
    # TODO handle case where the port is not open and sends hang
    context = zmq.Context()
    if pull:
        # for receiving socket messages
        socket = context.socket(zmq.PULL)
        socket.bind("tcp://127.0.0.1:%d" % port)
    else:
        # for sending socket messages
        socket = context.socket(zmq.PUSH)
        socket.connect("tcp://127.0.0.1:%d" % port)
    socket.setsockopt(zmq.LINGER, 0)
    return socket, context
Exemplo n.º 27
0
 def v2x_stat(self):
     socket.connect("tcp://192.168.123.253:5555")
     socket.setsockopt(zmq.SUBSCRIBE, b"wave")
     while 1:
         result = socket.recv()
         topic = result.decode()
         if (topic.startswith('ff')):
             try:
                 _, self.time_a, self.state_a, self.time_a, self.state_b, _ = topic.split(
                     '//')
                 print("Parsed")
             except:
                 print("Split Failed")
Exemplo n.º 28
0
 def send_have_message(self, index):
     have_message = bytearray(9)
     have_message[0:4] = struct.pack('>i', int(5))
     have_message[4] = struct.pack('>i', int(4))
     have_message[5:9] = struct.pack('>i', int(index))
     for listener in listen_list:
         print("Sending have message ", have_message, "to ", listener)
         socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         socket.connect(listener)
         socket.send(have_message)
         socket.close()
     return
Exemplo n.º 29
0
Arquivo: aom.py Projeto: thcoffee/aom
 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'})
Exemplo n.º 30
0
def first_user():
    #first function, asks for interface to listen on and opens server and presents current IP. then with return data calls "second function" to connect.
    socket = open_connection()
    socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    socket.bind((HOST, port_1))
    socket.listen(10)
 
    # add server socket object to the list of readable connections
    SOCKET_LIST.append(socket)
 
    print "Chat server started on port " + str(PORT)

    #return the socket
    return socket
Exemplo n.º 31
0
    def send(self, data):
        if type(data) is not dict:
            return False

        data = json.dumps(data)
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.setsockopt( zmq.RCVTIMEO, 500 )
        socket.connect("tcp://"+self.out_host+":"+self.out_port)
        print("IDEM SENDOVAT")
        socket.send_string(data)
        print("SENDNUTE")
        message = socket.recv()
        print(message)
Exemplo n.º 32
0
	def run(self):
		
		socket = zmq_context.socket(zmq.SUB)

		socket.connect ("epgm://"+ UDP_IP +":" + str(UDP_PORT))
		socket.setsockopt(zmq.SUBSCRIBE,'')
		
		while True:
			data = socket.recv()
			msg = data.split(':')
			msg.insert(0, str(datetime.now()))
			
			self.latest_messages.insert(0,msg)
			self.latest_messages = self.latest_messages[:keep_msg]
			self.msg_queue.append(msg)
Exemplo n.º 33
0
def backgroundZmqXmlrtListener():
    print "Subscribe to xMLrt updates %s" % g_zmqXSubUrl
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.connect("tcp://%s" % g_zmqXSubUrl)
    socket.setsockopt(zmq.SUBSCRIBE, "")

    while True:
        try:
            #  Wait for next request from client
            message = socket.recv()
            #print("Received xmlrt request: %s" % message)
            MyWebSocketHandler.broadcastMessage("xmlrt", message)
        except:
            pass
Exemplo n.º 34
0
    def MakeSocket(self,
                   context,
                   url=None,
                   type=None,
                   bind=None,
                   options=None,
                   pre_delay=None,
                   post_delay=None):
        """Create the socket.

    Arguments take precendence over their corresponding object attributes.

    """
        if type == None:
            type = self.type
        if url == None:
            url = self.url
        if bind == None:
            bind = self.bind
        if options == None:
            options = {}
        if self.options != None:
            if options == None:
                options = self.options
            else:
                options = dict(options.items() + self.options.items())
        if pre_delay == None:
            pre_delay = self.pre_delay
        if post_delay == None:
            post_delay = self.post_delay
        assert type is not None
        socket = context.socket(type)
        for k, v in options.items():
            socket.setsockopt(k, v)
        if pre_delay != None:
            time.sleep(pre_delay)
        assert url is not None
        if bind:
            logging.info("Binding %s socket to %s with context %s" %
                         (SocketTypeToString(type), url, hash(context)))
            socket.bind(url)
        else:
            logging.info("Connecting %s socket to %s with context %s" %
                         (SocketTypeToString(type), url, hash(context)))
            socket.connect(url)
        if post_delay != None:
            time.sleep(post_delay)
        return socket
Exemplo n.º 35
0
def echo_server(port):
    "A simple echo server"
    #Create a TCP socket
    sock = socket.setsockopt(socket.AF_INET, socket.SOCK_STREAM)
    #Enable reuse address/port
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    #Bind the socket to the port
    server_address = (host, port)
    print("starting up echo server on %s port %s" % server_address)
    sock.bind(server_address)
    #Listen to clients, backlog argument specifies the max no. of queued connections
    sock.listen(backlog)
    while True:
        print("Waiting to receive message from client")
        client, address = sock.accept()
        data = client.recv(data_payload)
        if data:
            print("Data: %s" % data)
            client.send(data)
            print("sent %s bytes back to %s" % (data, address))
            #end connection
            client.close()
            if _name_ == '_main_':
                parser = argparse.ArgumentParser(
                    description='Socket Server Example')
                parser.add_argument('--port',
                                    action="store",
                                    dest="port",
                                    type=int,
                                    required=True)
                given_args = parser.parse_args()
                port = given_args.port
                echo_server(port)
Exemplo n.º 36
0
	def run(self):
		context = zmq.Context()
		print "Starting Low-latency Service"
		socket = context.socket(zmq.SUB)
		socket.setsockopt (zmq.SUBSCRIBE, '')
		socket.connect ("tcp://" + _meg_device_ip + ":%s" % _port_low_latency)
		print "Started, listening for messages (SUB) from: " + _meg_device_ip + "\n"

        	while True:
			sleep(0.02)
			message = socket.recv()
			print message
			if message is None:
				pass
			else:
				self.queue.put(message)
				print "MESSAGE PUT: " + message + "\n"
Exemplo n.º 37
0
def backgroundZmqCaffeListener(uri, webSocketQ):
  print "Subscribe to C++ updates %s" % uri
  context = zmq.Context()
  socket = context.socket(zmq.SUB)
  socket.connect("tcp://%s" % uri)
  socket.setsockopt(zmq.SUBSCRIBE, "")

  while True:
    try:
      #  Wait for next request from client
      message = socket.recv()
      #print("Received caffe request: %s" % message)
      webSocketQ.put(("caffe", message))
#      MyWebSocketHandler.broadcastMessage("caffe", message)
    except:
      print ("Got an exception in backgroundZmqCaffeListener", uri)
      pass
Exemplo n.º 38
0
    def __init__(self, port, callback_obj, ttl=1, enable_loopback=False, bind_addr=''):
        asyncore.dispatcher.__init__(self)
        # self.lock = threading.RLock()
        self.MAX_MTU = 1500
        self.callback_obj = None
        self.port = port
        self.multicastSet = Set([])
        self.lock = threading.RLock()
        self.ttl = ttl
        self.enable_loopback = enable_loopback
        if callback_obj is not None and isinstance(callback_obj, IUdpCallback):
            self.callback_obj = callback_obj
        else:
            raise Exception('callback_obj is None or not an instance of IUdpCallback class')
        try:
            self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.set_reuse_addr()
            try:
                socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            except AttributeError:
                pass  # Some systems don't support SO_REUSEPORT

            # for both SENDER and RECEIVER to restrict the region
            self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self.ttl)
            # for SENDER to choose whether to use loop back
            if self.enable_loopback:
                self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
            else:
                self.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)

            self.bind_addr = bind_addr
            if self.bind_addr is None or self.bind_addr == '':
                self.bind_addr = socket.gethostbyname(socket.gethostname())
                # for both SENDER and RECEIVER to bind to specific network adapter
            self.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.bind_addr))

            # for RECEIVE to receive from multiple multicast groups
            self.bind(('', port))
        except Exception as e:
            print e
            traceback.print_exc()
        self.sendQueue = Queue.Queue()  # thread-safe queue
        AsyncController.instance().add(self)
        if self.callback_obj is not None:
            self.callback_obj.on_started(self)
Exemplo n.º 39
0
    def _reply_heartbeat(self, target):
        """Worker will kill its jobs when it lost connection with the master.
        """

        socket = self.ctx.socket(zmq.REP)
        socket.linger = 0
        socket.setsockopt(zmq.RCVTIMEO,
                          remote_constants.HEARTBEAT_RCVTIMEO_S * 1000)
        heartbeat_master_port =\
            socket.bind_to_random_port("tcp://*")
        self.master_heartbeat_address = "{}:{}".format(self.worker_ip,
                                                       heartbeat_master_port)

        logger.set_dir(
            os.path.expanduser('~/.parl_data/worker/{}'.format(
                self.master_heartbeat_address.replace(':', '_'))))

        self.heartbeat_socket_initialized.set()
        logger.info("[Worker] Connect to the master node successfully. "
                    "({} CPUs)".format(self.cpu_num))
        while self.master_is_alive and self.worker_is_alive:
            try:
                message = socket.recv_multipart()
                worker_status = self._get_worker_status()
                socket.send_multipart([
                    remote_constants.HEARTBEAT_TAG,
                    to_byte(str(worker_status[0])),
                    to_byte(str(worker_status[1])),
                    to_byte(worker_status[2]),
                    to_byte(str(worker_status[3]))
                ])
            except zmq.error.Again as e:
                self.master_is_alive = False
            except zmq.error.ContextTerminated as e:
                break
        socket.close(0)
        logger.warning(
            "[Worker] lost connection with the master, will exit reply heartbeat for master."
        )
        self.worker_status.clear()
        self.log_server_proc.kill()
        self.log_server_proc.wait()
        # exit the worker
        self.worker_is_alive = False
        self.exit()
Exemplo n.º 40
0
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
Exemplo n.º 41
0
  def MakeSocket(self, context, url = None, type = None, bind = None,
      options = None, pre_delay = None, post_delay = None):
    """Create the socket.

    Arguments take precendence over their corresponding object attributes.

    """
    if type == None:
      type = self.type
    if url == None:
      url = self.url
    if bind == None:
      bind = self.bind
    if options == None:
      options = {}
    if self.options != None:
      if options == None:
        options = self.options
      else:
        options = dict(options.items() + self.options.items())
    if pre_delay == None:
      pre_delay = self.pre_delay
    if post_delay == None:
      post_delay = self.post_delay
    assert type is not None
    socket = context.socket(type)
    for k, v in options.items():
      socket.setsockopt(k, v)
    if pre_delay != None:
      time.sleep(pre_delay)
    assert url is not None
    if bind:
      logging.info("Binding %s socket to %s with context %s" % (
          SocketTypeToString(type), url, hash(context)))
      socket.bind(url)
    else:
      logging.info("Connecting %s socket to %s with context %s" % (
          SocketTypeToString(type), url, hash(context)))
      socket.connect(url)
    if post_delay != None:
      time.sleep(post_delay)
    return socket
Exemplo n.º 42
0
    def _set_high_water_mark(self, socket):
        """ Set a high water mark on the zmq socket.  Do so in a way that is
        cross-compatible with zeromq2 and zeromq3.
        """

        if self.c['high_water_mark']:
            if hasattr(zmq, 'HWM'):
                # zeromq2
                socket.setsockopt(zmq.HWM, self.c['high_water_mark'])
            else:
                # zeromq3
                socket.setsockopt(zmq.SNDHWM, self.c['high_water_mark'])
                socket.setsockopt(zmq.RCVHWM, self.c['high_water_mark'])
Exemplo n.º 43
0
# Configure logging to file:
logfile_handler = logging.handlers.WatchedFileHandler(filename=args.destfile)
logfile_handler.setLevel(logging.INFO)

# Configure logging to stderr:
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter('%(asctime)s\t%(message)s'))
console_handler.setLevel(logging.DEBUG)  # Don't pollute log files with status

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.addHandler(logfile_handler)
log.addHandler(console_handler)
log.debug('Started. Logging to %s.' % args.destfile)


#
# Configure ZeroMQ Subscriber
#

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(args.publisher)
socket.setsockopt(zmq.IDENTITY, args.sid.encode('utf8'))
socket.setsockopt(zmq.SUBSCRIBE, args.topic.encode('utf8'))
log.debug('Connected to %s/%s' % (args.publisher, args.topic))


while 1:
    log.info(socket.recv().rstrip())
Exemplo n.º 44
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-


from devinclude import *
from bustime.models import *
import socket
import zmq
import cPickle as pickle
import time
import base64

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://127.0.0.1:15556")
#socket.connect("ipc://bustime_out")
#socket.connect("ipc://bustime_in")
#socket.setsockopt_string(zmq.SUBSCRIBE, u"")
socket.setsockopt(zmq.SUBSCRIBE, "")
#socket.setsockopt(zmq.SUBSCRIBE, "bdata_mode0")
#socket.setsockopt_string(zmq.SUBSCRIBE, u"busamounts_4")

while 1:
    sr = socket.recv()
    what, p = sr.split(' ', 1)
    p = pickle.loads(p)
    #p = pickle.loads(base64.b64decode(p))
    print what
    #print p
    print ""
Exemplo n.º 45
0
def send_msg(msg, socket=None, server=None,  querry_timeout=5, info=0):
    """
        return the result of running the task *runnable* with the given 
        arguments.
        
        params: 
            host: e.g. '210.45.117.30' or 'qtg7501' if use the later should add ip hostname pair in /etc/hosts
            querry:  querry whether server available 
            querry_timeout: 
                我曾经试过用 stopit module 来给recv设置timeout, 但是没有成功,应该是涉及到背后线程没有关闭
                refer to https://github.com/zeromq/pyzmq/issues/132
    """
    if socket is None: 
        assert server is not None 
        context, socket = conn_server(server, info=info)
    
    if 1: 
        socket.setsockopt(zmq.LINGER, 0)   #this is needed or else timeout wont work 
        #socket.send_pyobj('querry')    
        socket.send_pyobj(msg)    
        # use poll for timeouts:
        poller = zmq.Poller()
        poller.register(socket, zmq.POLLIN)
        if poller.poll(querry_timeout*1000): # 10s timeout in milliseconds
            reply = socket.recv_pyobj()
        else:
            #raise IOError("Timeout processing auth request")        
            #reply = None
            reply = 'not_reachable'

        if 0:  # below not working  
            try: 
                with stopit.SignalTimeout(querry_timeout, False) as ctx:
                #with stopit.ThreadingTimeout(querry_timeout, False) as ctx:
                    print 'tttttry', port, host  
                    server_status = socket.recv_pyobj()
            except Exception as err: 
                print 'rrrraise', err 
                #socket.close()
                #context.term()
                #raise 
                #server_status = 'not_reachable'
                raise 
            print 'sssssss', ctx.state    
            if ctx.state == ctx.EXECUTED:
                pass # All's fine, everything was executed within 10 seconds
            elif ctx.state == ctx.EXECUTING:
                pass # Hmm, that's not possible outside the block
            elif ctx.state == ctx.TIMED_OUT:
                server_status = 'not recheable' # Eeek the 10 seconds timeout occurred while executing the block
            elif ctx.state == ctx.INTERRUPTED:
                pass 
                # Oh you raised specifically the TimeoutException in the block
            elif ctx.state == ctx.CANCELED:
                pass # Oh you called to_ctx_mgr.cancel() method within the block but it # executed till the end
            else:
                pass 
                # That's not possible            
            #print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.state == ctx.EXCUTING 
            print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.TIMED_OUT,  
    
    
    return reply 
Exemplo n.º 46
0
                print ti + "] Reply is bad (unexpected). Discarding."

            callback_registry_lock.release()

        
def mycallback(msg):
    print "in callback"
    
routerport = 8123
myname = "ZMQDEALERDEMO"

context = zmq.Context()
print "Create DEALER socket on " + str(routerport)
socket = context.socket(zmq.DEALER)
socket.connect("tcp://localhost:" + str(routerport))
socket.setsockopt(zmq.IDENTITY, myname)

thread = threading.Thread(target=handler, args=(socket,))
thread.start()

print "main] loop running"

while True:
    time.sleep(1)
    
    # randomly indicate that we'd like a reply. if we want one, start a thread to wait for it
    # the timestamp is the message ID
    
    reply_wanted = random.randint(0,1)
    msg_id = str(time.time())
    if reply_wanted == 0:
Exemplo n.º 47
0
def main():
    argparser = argparse.ArgumentParser()
    argparser.add_argument(
        '-u',
        action='store',
        dest='videofeed_url',
        help='the url of the videofeed',
        default='http://192.198.1.1:8080/videofeed')
    argparser.add_argument(
        '-l',
        action='store',
        dest='listen_port',
        type=int,
        help='the port on which this server listens to instructions',
        default='9010')
    argparser.add_argument(
        '-p',
        action='store',
        dest='output_port',
        type=int,
        help='the port on which this server retransmits',
        default='9100')
    argparser.add_argument(
        '--no-zmq',
        action='store_true',
        help='Disable zmq socket when running the server.',
        default=False)
    argparser.add_argument(
        '--pid-file',
        action='store',
        dest='pid_file',
        help='the file in which we store the pid of the webserver',
        default=None)
    argparser.add_argument(
        '--dump-file',
        help='If set dump a few frames in the file as json,'
        ' to be used as a fake url.',
        default=None)
    arguments = argparser.parse_args()
    if (arguments.pid_file is not None):
        logging.basicConfig(
            filename=arguments.pid_file + '.log',
            level=logging.DEBUG)
        with open(arguments.pid_file, "w") as pidfile:
            pidfile.write(str(os.getpid()))

    VideoHandler.use_zmq = not arguments.no_zmq
    if (VideoHandler.use_zmq):
        import zmq
        context = zmq.Context()
        socket = context.socket(zmq.REP)
        socket.setsockopt(zmq.LINGER, 1)
        VideoHandler.socket = socket
        logging.info(
            "video server zmq bind on port " + str(arguments.listen_port))
        try:
            socket.bind("tcp://*:%i" % (arguments.listen_port))
        except Exception as exc:
            logging.error(exc)
            import sys
            netstat()
            sys.exit(-1)
        # if we do not wait the first messages are lost
        import time
        time.sleep(0.6)
    if (arguments.dump_file is not None):
        logging.info("dump to a file")
        dump_to_file(arguments.videofeed_url, arguments.dump_file)
    else:
        VideoHandler.url = arguments.videofeed_url
        server = ThreadedHTTPServer(('', arguments.output_port), VideoHandler)
        logging.info("Start server")
        server.serve_forever()
Exemplo n.º 48
0
# import necessary modules
from tkFileDialog import askopenfilename
try: import cPickle as pickle
except ImportError: import pickle
# added modules for eye control
from pygame.locals import *
import pygame, random, sys, ezmenu, level_editor, zmq, socket, sys, csv, select, json

# network setup for Pupil Labs data stream
port = "5000"
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://127.0.0.1:"+port)

# filter by messages by stating string 'STRING'. '' receives all messages
socket.setsockopt(zmq.SUBSCRIBE, 'gaze_positions')

delim = ';'
times=[]

# larry is the name of the computer (not a person...)
pos_name = 'realtime gaze on larry' 

class Block(pygame.sprite.Sprite):
    """ A breakout block """
    def __init__(self, image, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = image.get_rect()
        self.x = x; self.y = y
Exemplo n.º 49
0
#!/usr/bin/python

__author__ = "Seamus"

import socket

port = 9000
size = 512
host = ''

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

socket.bind(host, port)
socket.listen(10)

conn, address = socket.accept()
data = conn.recv(size)
if data:
	f = open("storage.dat", address[0])
	print("connection is from: ". address[0])
	f.write(address[0])
	f.write(":")
	f.write(data.decode("utf-8"))
	f.close()
socket.close()


Exemplo n.º 50
0
GPIO.setup(14, GPIO.OUT) # GREEN
GPIO.setup(15, GPIO.OUT) # RED
GPIO.setup(18, GPIO.OUT) # SERVO
GPIO.setup(23, GPIO.OUT) # BLUE

GPIO.output(15, GPIO.LOW) # RED
GPIO.output(14, GPIO.LOW) # GREEN
GPIO.output(23, GPIO.LOW) # BLUE
pwm = GPIO.PWM(18, 50)
pwm.start(7.5)
##################################################

################### Socket Setting ###############
signal.signal(signal.SIGINT , sinal_handler)
socket = socket(AF_INET , SOCK_STREAM)
socket.setsockopt(SOL_SOCKET , SO_REUSEADDR , 1)

port = 7500


socket.bind(('' , port))
socket.listen(5)

##################################################

############## Message recieve and send image########
# msg fotmat delay/send_mail/num_of_picture/back_setting/On_setting
# Or b+ b0 b- m+ m0 m- BG0 BG1 BG2 BG3 BG4 
def fileSend():
 global client
 global frame
Exemplo n.º 51
0
def queue(runnable, args=None, kwargs=None, querry=False, 
        host=None, port=None, tunnel=False, querry_timeout=5, 
        tunnel_server=None, 
        kill_server=False, ):
    """
        return the result of running the task *runnable* with the given 
        arguments.
        
        params: 
            host: e.g. '210.45.117.30' or 'qtg7501' if use the later should add ip hostname pair in /etc/hosts
            querry:  querry whether server available 
            querry_timeout: 
                我曾经试过用 stopit module 来给recv设置timeout, 但是没有成功,应该是涉及到背后线程没有关闭
                refer to https://github.com/zeromq/pyzmq/issues/132
    """
    #host =  '222.195.73.70'
    #port = 90900
    host = host if host is not None else '127.0.0.1'
    port = port if port is not None else 90900
    args = args if args is not None else ()
    kwargs = kwargs if kwargs is not None else {}
    
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    url = 'tcp://{}:{}'.format(host, port)
    if not tunnel:  #one should either use connect or tunnel_connection, not both.
        socket.connect(url)
    else:
        #zmq.ssh.tunnel_connection(socket, url, "myuser@remote-server-ip")
        #issue:  似乎tunnel对port有限制,不能用90900这样5位数的的端口
        zmq.ssh.tunnel_connection(socket, url, tunnel_server)
        #print 'tunnel succeed: {}'.format(url)
       
    
    if kill_server: 
        socket.send_pyobj({'header': 'stop'}) 
        rep=server_status = socket.recv_pyobj()
        print 'REP: %s'%(rep, )
        return 
        
    results = None 
    status = 'refuse'
    
    if querry:
        socket.setsockopt(zmq.LINGER, 0)   #this is needed or else timeout wont work 
        #socket.send_pyobj('querry')    
        num_of_threads = None
        num_of_memory = None 
        if len(args)>0:  #in main.Main.run_many_dist, NUM_OF_THREADS is passed in args[0]
            if isinstance(args[0], dict): 
                num_of_threads = args[0].get('NUM_OF_THREADS')
                num_of_memory = args[0].get('num_of_memory', None) 
        socket.send_pyobj({
            'header': 'querry', 
            'what': 'is_available', 
            #'num_of_threads': kwargs.get('NUM_OF_THREADS', None),   # requested resources 
            #'num_of_memory': kwargs.get('num_of_memory', None), 
            'num_of_threads': num_of_threads, 
            'num_of_memory': num_of_memory, 
            })    
        # use poll for timeouts:
        poller = zmq.Poller()
        poller.register(socket, zmq.POLLIN)
        if poller.poll(querry_timeout*1000): # 10s timeout in milliseconds
            server_status = socket.recv_pyobj()
            #print_vars(vars(),  ['server_status'])
        else: 
            
            #raise IOError("Timeout processing auth request")        
            #not able to reach server within querry_timeout 
            #some times, need to enlarge querry_timeout to ensure connection success 
            server_status = 'not_reachable'
            status = 'conn timeout'

        if 0:  # below not working  
            try: 
                with stopit.SignalTimeout(querry_timeout, False) as ctx:
                #with stopit.ThreadingTimeout(querry_timeout, False) as ctx:
                    print 'tttttry', port, host  
                    server_status = socket.recv_pyobj()
            except Exception as err: 
                print 'rrrraise', err 
                #socket.close()
                #context.term()
                #raise 
                #server_status = 'not_reachable'
                raise 
            print 'sssssss', ctx.state    
            if ctx.state == ctx.EXECUTED:
                pass # All's fine, everything was executed within 10 seconds
            elif ctx.state == ctx.EXECUTING:
                pass # Hmm, that's not possible outside the block
            elif ctx.state == ctx.TIMED_OUT:
                server_status = 'not recheable' # Eeek the 10 seconds timeout occurred while executing the block
            elif ctx.state == ctx.INTERRUPTED:
                pass 
                # Oh you raised specifically the TimeoutException in the block
            elif ctx.state == ctx.CANCELED:
                pass # Oh you called to_ctx_mgr.cancel() method within the block but it # executed till the end
            else:
                pass 
                # That's not possible            
            #print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.state == ctx.EXCUTING 
            print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.TIMED_OUT,  
    
    else:   
        server_status = 'available'
        
    if server_status == 'available': 
        #runnable_string = cloud.serialization.cloudpickle.dumps(runnable)
        #socket.send_pyobj({'header': 'run', 
        #    'runnable_string': runnable_string, 
        #    'args': args, 
        #    'kwargs': kwargs
        #    })    
        msg = pack_runnable_msg(runnable, args, kwargs)
        socket.send_pyobj(msg)
       
        results = socket.recv_pyobj()
        status = 'done'
    else: 
        if server_status != 'not_reachable':
            status += '  %s'%(server_status, ) 


    
    # these are not necessary, but still good practice:
    socket.close()
    context.term()    
    return status, results
Exemplo n.º 52
0
    def check_channel_request(self, kind, chanid):
        if kind == 'session':
            return paramiko.OPEN_SUCCEEDED
        return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

    def check_auth_password(self, username, password):
        if username == '' and password == '':
            return paramiko.AUTH_SUCCESSFUL
        return paramiko.AUTH_FAILED

server = sys.argv[1]
ssh_port = int(sys.argv[2])

try:
    sock = s.socket(s.AF_INET, s.SOCK_STREAM)
    s.setsockopt(s.SOL_SOCKET, s.SO_REUSEADDR, 1)
    sock.bind((server, ssh_port))
    sock.listen(100)
    print("[+] Listening for connection... ")
    client, addr = sock.accept()

except Exception as e:
    print("[-]  Listen failed: " + str(e))
    sys.exit(1)
print("[+] got a connection")

try:
    Session = paramiko.Transport(client)
    Session.add_server_key(host_key)
    server = Server()
    try:
Exemplo n.º 53
0
def conn_server(server, info=0): 
    """
        return the result of running the task *runnable* with the given 
        arguments.
        
        params: 
            host: e.g. '210.45.117.30' or 'qtg7501' if use the later should add ip hostname pair in /etc/hosts
            querry:  querry whether server available 
            querry_timeout: 
                我曾经试过用 stopit module 来给recv设置timeout, 但是没有成功,应该是涉及到背后线程没有关闭
                refer to https://github.com/zeromq/pyzmq/issues/132
    """
    server_info = resolve_server(server)
    host = server_info['host']
    port = server_info['port']
    tunnel = server_info['tunnel']
    tunnel_server = server_info['tunnel_server']
    
    if info>0: 
        print_vars(vars(),  ['server_info'])
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    url = 'tcp://{}:{}'.format(host, port)
    if not tunnel:  #one should either use connect or tunnel_connection, not both.
        socket.connect(url)
    else:
        #zmq.ssh.tunnel_connection(socket, url, "myuser@remote-server-ip")
        #issue:  似乎tunnel对port有限制,不能用90900这样5位数的的端口
        zmq.ssh.tunnel_connection(socket, url, tunnel_server)
        #print 'tunnel succeed: {}'.format(url)
    
    if 0: 
        socket.setsockopt(zmq.LINGER, 0)   #this is needed or else timeout wont work 
        #socket.send_pyobj('querry')    
        socket.send_pyobj({'header': 'querry'})    
        # use poll for timeouts:
        poller = zmq.Poller()
        poller.register(socket, zmq.POLLIN)
        if poller.poll(querry_timeout*1000): # 10s timeout in milliseconds
            server_status = socket.recv_pyobj()
        else:
            #raise IOError("Timeout processing auth request")        
            status = server_status = 'not_reachable'

        if 0:  # below not working  
            try: 
                with stopit.SignalTimeout(querry_timeout, False) as ctx:
                #with stopit.ThreadingTimeout(querry_timeout, False) as ctx:
                    print 'tttttry', port, host  
                    server_status = socket.recv_pyobj()
            except Exception as err: 
                print 'rrrraise', err 
                #socket.close()
                #context.term()
                #raise 
                #server_status = 'not_reachable'
                raise 
            print 'sssssss', ctx.state    
            if ctx.state == ctx.EXECUTED:
                pass # All's fine, everything was executed within 10 seconds
            elif ctx.state == ctx.EXECUTING:
                pass # Hmm, that's not possible outside the block
            elif ctx.state == ctx.TIMED_OUT:
                server_status = 'not recheable' # Eeek the 10 seconds timeout occurred while executing the block
            elif ctx.state == ctx.INTERRUPTED:
                pass 
                # Oh you raised specifically the TimeoutException in the block
            elif ctx.state == ctx.CANCELED:
                pass # Oh you called to_ctx_mgr.cancel() method within the block but it # executed till the end
            else:
                pass 
                # That's not possible            
            #print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.state == ctx.EXCUTING 
            print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.TIMED_OUT,  
    
    # these are not necessary, but still good practice:
    #socket.close()
    #context.term()    
    return context, socket 
Exemplo n.º 54
0
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
    socket.setsockopt(zmq.LINGER, 0)
    return ok
    
class RoboConnector:
    def __init__(self, ip, port):
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REQ)
        self.addr = "tcp://" + ip + ":" + str(port)
        self.socket.connect(self.addr)
        self.t0 = time.time() - 10000
        self.dataReady = False
    def get(self, dt=0.05):
        if (self.t0 + dt > time.time()) and (self.dataReady):
            return self.cache
        else:
            # print 'call'
Exemplo n.º 55
0
 def create_zmq_socket(self):
     self.log.info('Creating Socket')
     socket = self.ctx.socket(zmq.REQ)
     socket.setsockopt(zmq.LINGER, 0)
     # self._socket.setsockopt(zmq.SOCKS_PROXY, "127.0.0.1:9051");
     return socket