Пример #1
0
class TestUnboundTCPPort(protocol.ServerFactory, ValidateDataMixin):
    protocol = TCPTestProtocol

    def __init__(self, timeout):
        ValidateDataMixin.__init__(self, timeout)
        self.listener = None
        self.port = None

    def start_test(self, port, remoteHost, remotePort):
        self.port = port
        #listen for responses if necessary
        try:
            self.listener = Globals.reactor.listenTCP(port, self, interface="")
        except CannotListenError, e:
            self.cancel()
            return defer.fail(failure.Failure(e))

        #send off the request for a reply:
        self.outgoingProtocol = TCPTestProtocol()
        d = defer.Deferred()

        def on_connection_failed(reason):
            self.fail(reason)

        d.addErrback(on_connection_failed)
        outgoingFactory = protocol._InstanceFactory(Globals.reactor,
                                                    self.outgoingProtocol, d)
        Globals.reactor.connectTCP(remoteHost, remotePort, outgoingFactory,
                                   CONNECTION_TIMEOUT)

        return self.get_deferred()
Пример #2
0
 def connect(self, dn, overrides=None):
     """Connect to remote host, return Deferred of resulting protocol instance."""
     d = defer.Deferred()
     f = protocol._InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     c = LDAPConnector(self.reactor, dn, f, overrides=overrides)
     c.connect()
     return d
Пример #3
0
    def connect(self, host=None, port=None, spec=None, user=None, password=None, vhost=None,
            heartbeat=None, clientClass=None):
        host = host or self.host
        port = port or self.port
        spec = spec or self.spec
        user = user or self.user
        password = password or self.password
        vhost = vhost or self.vhost
        heartbeat = heartbeat or self.heartbeat
        clientClass = clientClass or self.clientClass

        delegate = TwistedDelegate()
        onConn = Deferred()
        p = clientClass(delegate, vhost, txamqp.spec.load(spec), heartbeat=heartbeat)
        f = protocol._InstanceFactory(reactor, p, onConn)
        c = reactor.connectTCP(host, port, f)
        def errb(thefailure):
            thefailure.trap(error.ConnectionRefusedError)
            print "failed to connect to host: %s, port: %s; These tests are designed to run against a running instance" \
                  " of the %s AMQP broker on the given host and port.  failure: %r" % (host, port, self.broker, thefailure,)
            thefailure.raiseException()
        onConn.addErrback(errb)

        self.connectors.append(c)
        client = yield onConn

        yield client.authenticate(user, password)
        returnValue(client)
Пример #4
0
 def connectSimpleProducer(self, name, timeout=30, bindName=None):
     """
     Simple Producer
     """
     d = defer.Deferred()
     f = _InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     self.p_reactor.connectSimpleProducer(name, f, timeout=timeout, bindAddress=bindName)
     return d
Пример #5
0
 def connect(self, dn, overrides=None, bindAddress=None):
     """Connect to remote host, return Deferred of resulting protocol instance."""
     d = defer.Deferred()
     f = protocol._InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     c = LDAPConnector(self.reactor, dn, f, overrides=overrides,
             bindAddress=bindAddress)
     c.connect()
     return d
Пример #6
0
def send_notification(s, xml):
    """
    send a notification a subscriber
    return its response
    """
    log_category = "notification_protocol"

    _,host_port,path,_,_ = urlsplit(s['callback'])
    if path == '':
        path = '/'
    if host_port.find(':') != -1:
        host,port = tuple(host_port.split(':'))
        port = int(port)
    else:
        host = host_port
        port = 80

    def send_request(p,port_item):
        request = ['NOTIFY %s HTTP/1.1' % path,
                    'HOST:  %s:%d' % (host, port),
                    'SEQ:  %d' % s['seq'],
                    'CONTENT-TYPE:  text/xml;charset="utf-8"',
                    'SID:  %s' % s['sid'],
                    'NTS:  upnp:propchange',
                    'NT:  upnp:event',
                    'Content-Length: %d' % len(xml),
                    '',
                    xml]

        request = '\r\n'.join(request)
        log.info(log_category, "send_notification.send_request to %r %r",
                 s['sid'], s['callback'])
        log.debug(log_category, "request: %r", request)
        s['seq'] += 1
        if s['seq'] > 0xffffffff:
            s['seq'] = 1
        p.transport.write(request)
        port_item.disconnect()
        #return p.transport.write(request)

    def got_error(failure,port_item):
        port_item.disconnect()
        log.info(log_category, "error sending notification to %r %r",
                 s['sid'], s['callback'])
        log.debug(log_category, failure)

    #c = ClientCreator(reactor, NotificationProtocol)
    #d = c.connectTCP(host, port)

    d = defer.Deferred()
    f = _InstanceFactory(reactor, NotificationProtocol(), d)
    port_item = reactor.connectTCP(host, port, f, timeout=30, bindAddress=None)

    d.addCallback(send_request, port_item)
    d.addErrback(got_error, port_item)

    return d,port_item
Пример #7
0
def send_notification(s, xml):
    """
    send a notification a subscriber
    return its response
    """
    log_category = "notification_protocol"

    _, host_port, path, _, _ = urlsplit(s["callback"])
    if path == "":
        path = "/"
    if host_port.find(":") != -1:
        host, port = tuple(host_port.split(":"))
        port = int(port)
    else:
        host = host_port
        port = 80

    def send_request(p, port_item):
        request = [
            "NOTIFY %s HTTP/1.1" % path,
            "HOST:  %s:%d" % (host, port),
            "SEQ:  %d" % s["seq"],
            'CONTENT-TYPE:  text/xml;charset="utf-8"',
            "SID:  %s" % s["sid"],
            "NTS:  upnp:propchange",
            "NT:  upnp:event",
            "Content-Length: %d" % len(xml),
            "",
            xml,
        ]

        request = "\r\n".join(request)
        log.info(log_category, "send_notification.send_request to %r %r", s["sid"], s["callback"])
        log.debug(log_category, "request: %r", request)
        s["seq"] += 1
        if s["seq"] > 0xFFFFFFFF:
            s["seq"] = 1
        p.transport.write(request)
        port_item.disconnect()
        # return p.transport.write(request)

    def got_error(failure, port_item):
        port_item.disconnect()
        log.info(log_category, "error sending notification to %r %r", s["sid"], s["callback"])
        log.debug(log_category, failure)

    # c = ClientCreator(reactor, NotificationProtocol)
    # d = c.connectTCP(host, port)

    d = defer.Deferred()
    f = _InstanceFactory(reactor, NotificationProtocol(), d)
    port_item = reactor.connectTCP(host, port, f, timeout=30, bindAddress=None)

    d.addCallback(send_request, port_item)
    d.addErrback(got_error, port_item)

    return d, port_item
Пример #8
0
def send_notification(s, xml):
    """
    send a notification a subscriber
    return its response
    """
    log_category = "notification_protocol"

    _,host_port,path,_,_ = urlsplit(s['callback'])
    if path == '':
        path = '/'
    if host_port.find(':') != -1:
        host,port = tuple(host_port.split(':'))
        port = int(port)
    else:
        host = host_port
        port = 80

    def send_request(p,port_item):
        request = ['NOTIFY %s HTTP/1.1' % path,
                    'HOST:  %s:%d' % (host, port),
                    'SEQ:  %d' % s['seq'],
                    'CONTENT-TYPE:  text/xml;charset="utf-8"',
                    'SID:  %s' % s['sid'],
                    'NTS:  upnp:propchange',
                    'NT:  upnp:event',
                    'Content-Length: %d' % len(xml),
                    '',
                    xml]

        request = '\r\n'.join(request)
        log.info(log_category, "send_notification.send_request to %r %r",
                 s['sid'], s['callback'])
        log.debug(log_category, "request: %r", request)
        s['seq'] += 1
        if s['seq'] > 0xffffffff:
            s['seq'] = 1
        p.transport.write(request)
        port_item.disconnect()
        #return p.transport.write(request)

    def got_error(failure,port_item):
        port_item.disconnect()
        log.info(log_category, "error sending notification to %r %r",
                 s['sid'], s['callback'])
        log.debug(log_category, failure)

    #c = ClientCreator(reactor, NotificationProtocol)
    #d = c.connectTCP(host, port)

    d = defer.Deferred()
    f = _InstanceFactory(reactor, NotificationProtocol(), d)
    port_item = reactor.connectTCP(host, port, f, timeout=30, bindAddress=None)

    d.addCallback(send_request, port_item)
    d.addErrback(got_error, port_item)

    return d,port_item
Пример #9
0
 def connect(self, dn, overrides=None, bindAddress=None, timeout=5):
     d = defer.Deferred()
     # noinspection PyProtectedMember
     f = protocol._InstanceFactory(
         self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     c = LDAPConnector(self.reactor, dn, f, overrides=overrides,
                       bind_address=bindAddress, timeout=timeout)
     c.connect()
     return d
Пример #10
0
def send_notification(s, xml):
    '''
    send a notification a subscriber
    return its response
    '''
    logger = log.get_logger('notification_protocol')
    # logger.debug('\t-send_notification s is: {}'.format(s))
    # logger.debug('\t-send_notification xml is: {}'.format(xml))

    _, host_port, path, _, _ = urlsplit(s['callback'])
    # logger.debug('\t-send_notification host_port is: {}'.format(host_port))
    # logger.debug('\t-send_notification path is: {}'.format(path))
    path = to_string(path)
    host_port = to_string(host_port)
    if path == '':
        path = '/'
    if host_port.find(':') != -1:
        host, port = tuple(host_port.split(':'))
        port = int(port)
    else:
        host = host_port
        port = 80

    def send_request(p, port_item):
        request = [
            f'NOTIFY {path} HTTP/1.1', f'HOST:  {host}:{port}',
            f'SEQ:  {s["seq"]}', 'CONTENT-TYPE:  text/xml;charset="utf-8"',
            f'SID:  {s["sid"]}', 'NTS:  upnp:propchange', 'NT:  upnp:event',
            f'Content-Length: {len(xml)}', ''
        ]
        request = [to_bytes(x) for x in request]
        request.append(xml)
        request = b'\r\n'.join(request)
        logger.info(f'send_notification.send_request to '
                    f'{s["sid"]} {s["callback"]}')
        logger.info(f'request: {request}')
        s['seq'] += 1
        if s['seq'] > 0xffffffff:
            s['seq'] = 1
        p.transport.write(request)
        port_item.disconnect()

    def got_error(failure, port_item):
        port_item.disconnect()
        logger.info(
            f'error sending notification to {s["sid"]} {s["callback"]}')
        logger.debug(failure)

    d = defer.Deferred()
    f = _InstanceFactory(reactor, NotificationProtocol(), d)
    port_item = reactor.connectTCP(host, port, f, timeout=30, bindAddress=None)

    d.addCallback(send_request, port_item)
    d.addErrback(got_error, port_item)

    return d, port_item
Пример #11
0
	def connectSocks5Proxy(self,remotehost,remoteport,proxy,proxyport,id):
	   d = defer.Deferred()
	   f = protocol._InstanceFactory(self.reactor,
	                                self.protocolClass(*self.args, **self.kwargs),d)
	   self.reactor.connectWith(ClientConnector,
			            host=remotehost,port=remoteport,
				    sockshost=proxy,socksport=proxyport,
				    otherFactory=f,
				    readableID=id)
	   return d
Пример #12
0
	def connectSocks5Proxy(self,remotehost,remoteport,proxy,proxyport,id):
	   d = defer.Deferred()
	   f = protocol._InstanceFactory(self.reactor,
	                                self.protocolClass(*self.args, **self.kwargs),d)
	   self.reactor.connectWith(ClientConnector,
			            host=remotehost,port=remoteport,
				    sockshost=proxy,socksport=proxyport,
				    otherFactory=f,
				    readableID=id)
	   return d
Пример #13
0
 def connectMS(self, address, timeout=30, bindAddress=None):
     """Connect to remote address (messaging service name), return
     a Deferred of resulting protocol instance.
     @param address 'Messaging service' remote address (name)
     @param bindAddress 'Messaging service' address (name) to listen on.
     @param timeout Error timeout, in seconds
     """
     d = defer.Deferred()
     f = _InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     self.p_reactor.connectMS(address, f, timeout=timeout, bindAddress=bindAddress)
     return d
Пример #14
0
 def connectWorkProducer(self, name, timeout=30, bindName=None):
     """
     Distributed work producer client (producer end of worker-queue
     pattern)
     return a Deferred of resulting protocol instance.
     @param name "Exchange Point/Distributed Topic"
     """
     d = defer.Deferred()
     f = _InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     self.p_reactor.connectWorkProducer(name, f, timeout=timeout, bindAddress=bindName)
     return d
Пример #15
0
 def connect(self, dn, overrides=None, bindAddress=None, timeout=5):
     d = defer.Deferred()
     # noinspection PyProtectedMember
     f = protocol._InstanceFactory(
         self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     c = LDAPConnector(self.reactor,
                       dn,
                       f,
                       overrides=overrides,
                       bind_address=bindAddress,
                       timeout=timeout)
     c.connect()
     return d
Пример #16
0
 def connectTCP(self, host, port, timeout=30, bindAddress=None):
     """Connect to remote Broker host, return a Deferred of resulting protocol
     instance.
     """
     d = defer.Deferred()
     p = self.amqpProtocol(self.amqpDelegate(), 
                                 self.vhost,
                                 self.spec)
     # p.factory = self
     f = protocol._InstanceFactory(self.reactor, p, d)
     self.connector = self.reactor.connectTCP(host, port, f, timeout=timeout,
             bindAddress=bindAddress)
     d.addCallback(self._broker_login)
     return d
Пример #17
0
    def connect(self, host=None, port=None, spec=None, user=None, password=None, vhost=None):
        host = host or self.host
        port = port or self.port
        spec = spec or self.spec
        user = user or self.user
        password = password or self.password
        vhost = vhost or self.vhost

        delegate = TwistedDelegate()
        onConn = Deferred()
        f = protocol._InstanceFactory(reactor, AMQClient(delegate, vhost, txamqp.spec.load(spec)), onConn)
        c = reactor.connectTCP(host, port, f)
        self.connectors.append(c)
        client = yield onConn

        yield client.authenticate(user, password)
        returnValue(client)
Пример #18
0
 def connectTCP(self, host, port, timeout=30, bindAddress=None):
     """Connect to remote Broker host, return a Deferred of resulting protocol
     instance.
     """
     d = defer.Deferred()
     p = self.protocol(self.delegate,
                                 self.vhost,
                                 self.spec,
                                 heartbeat=self.heartbeat)
     p.factory = self
     f = protocol._InstanceFactory(self.reactor, p, d)
     self.connector = self.reactor.connectTCP(host, port, f, timeout=timeout,
             bindAddress=bindAddress)
     def auth_cb(conn):
         d = conn.authenticate(self.username, self.password)
         d.addCallback(lambda _: conn)
         return d
     d.addCallback(auth_cb)
     return d
Пример #19
0
 def connectSocks5Proxy(self,
                        remotehost,
                        remoteport,
                        proxy,
                        proxyport,
                        id,
                        proxylogin=None,
                        proxypass=None):
     d = defer.Deferred()
     f = protocol._InstanceFactory(
         self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
     c = ClientConnector(host=remotehost,
                         port=remoteport,
                         sockshost=proxy,
                         socksport=proxyport,
                         otherFactory=f,
                         reactor=self.reactor,
                         login=proxylogin,
                         password=proxypass)
     return c
Пример #20
0
    def connect(self):
        host = self.host
        port = self.port
        spec = self.spec
        user = self.username
        password = self.password
        vhost = self.vhost
        delegate = TwistedDelegate()
        onConn = Deferred()
        p = AMQClient(delegate, vhost, txamqp.spec.load(spec), heartbeat=0)
        f = protocol._InstanceFactory(reactor, p, onConn)
        c = reactor.connectTCP(host, port, f)

        def errb(thefailure):
            thefailure.trap(error.ConnectionRefusedError)
            logging.error(traceback.format_exc())
        onConn.addErrback(errb)
        client = yield onConn
        self.client = client
        yield self.authenticate(self.client, user, password)
        returnValue(client)
Пример #21
0
    def connect(self,
                host=None,
                port=None,
                spec=None,
                user=None,
                password=None,
                vhost=None,
                heartbeat=None,
                clientClass=None):
        host = host or self.host
        port = port or self.port
        spec = spec or self.spec
        user = user or self.user
        password = password or self.password
        vhost = vhost or self.vhost
        heartbeat = heartbeat or self.heartbeat
        clientClass = clientClass or self.clientClass

        delegate = TwistedDelegate()
        onConn = Deferred()
        p = clientClass(delegate,
                        vhost,
                        txamqp.spec.load(spec),
                        heartbeat=heartbeat)
        f = protocol._InstanceFactory(reactor, p, onConn)
        c = reactor.connectTCP(host, port, f)

        def errb(thefailure):
            thefailure.trap(error.ConnectionRefusedError)
            print "failed to connect to host: %s, port: %s; These tests are designed to run against a running instance" \
                  " of the %s AMQP broker on the given host and port.  failure: %r" % (host, port, self.broker, thefailure,)
            thefailure.raiseException()

        onConn.addErrback(errb)

        self.connectors.append(c)
        client = yield onConn

        yield self.authenticate(client, user, password)
        returnValue(client)
Пример #22
0
    def connect(self):
        host = self.host
        port = self.port
        spec = self.spec
        user = self.username
        password = self.password
        vhost = self.vhost
        delegate = TwistedDelegate()
        onConn = Deferred()
        p = AMQClient(delegate, vhost, txamqp.spec.load(spec), heartbeat=0)
        f = protocol._InstanceFactory(reactor, p, onConn)
        c = reactor.connectTCP(host, port, f)

        def errb(thefailure):
            thefailure.trap(error.ConnectionRefusedError)
            logging.error(traceback.format_exc())

        onConn.addErrback(errb)
        client = yield onConn
        self.client = client
        yield self.authenticate(self.client, user, password)
        returnValue(client)
Пример #23
0
def clientCreatorConnectUTP(self, host, port, timeout=30, bindAddress=None):
    """Connect to remote host, return Deferred of resulting protocol instance."""
    d = defer.Deferred()
    f = _InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
    self.reactor.connectUTP(host, port, f, timeout=timeout, bindAddress=bindAddress)
    return d
Пример #24
0
	def connectSocks5Proxy(self,remotehost,remoteport,proxy,proxyport,id,proxylogin=None,proxypass=None):
	   d = defer.Deferred()
	   f = protocol._InstanceFactory(self.reactor,self.protocolClass(*self.args, **self.kwargs),d)
	   c = ClientConnector(host=remotehost,port=remoteport,sockshost=proxy,socksport=proxyport,otherFactory=f,reactor=self.reactor, login=proxylogin, password=proxypass)
	   return c