Exemplo n.º 1
0
        endpoint = TCP4ClientEndpoint(self._reactor, host, port)
        senderProto = LineSender(host.encode('ascii'))
        connectProtocol(endpoint, senderProto)


class IPGetter(LineOnlyReceiver):
    delimiter = b'\n'
    def __init__(self, sender):
        self._sender = sender

    def lineReceived(self, line):
        try:
            port = int(line)
        except ValueError:
            self.transport.loseConnection()
            return
        addr = self.transport.getPeer()
        self.sendIP(addr.host, port)

    def sendIP(self, host, port):
        self._sender.sendIP(host, port)
        self.transport.loseConnection()


if __name__ == '__main__':
    sender = IPSender(reactor)
    endpoint = TCP4ServerEndpoint(reactor, 10050)
    endpoint.listen(ArgumentsFactory.forProtocol(IPGetter, sender))
    log.startLogging(sys.stdout)
    reactor.run()
Exemplo n.º 2
0
def makeServer(reactor):
    endpoint = TCP4ServerEndpoint(reactor, 0)
    finished = Deferred()
    return (endpoint.listen(ArgumentsFactory.forProtocol(IPReceiver, finished))
        .addCallback(lambda listeningPort: (finished, listeningPort.getHost().port))
    )
Exemplo n.º 3
0
def makeServer(reactor):
    endpoint = TCP4ServerEndpoint(reactor, 0)
    finished = Deferred()
    return (endpoint.listen(ArgumentsFactory.forProtocol(
        IPReceiver, finished)).addCallback(
            lambda listeningPort: (finished, listeningPort.getHost().port)))