Listing 4. Scheduling Events in Twisted

def outgoingConnectionMade(self, outgoing_proxy,
                           uri):
    """
    This occurs when our outbound proxy has
    connected. It's a Twisted callback method.
    """
    assert(outgoing_proxy, OutgoingProxy)
    self.outgoing_proxy = outgoing_proxy
    outgoing_proxy.incoming_proxy = self

    # Send HTTP command and echo back result
    outgoing_proxy.write('%s %s %s' % \
        (self.http_command,
         uri,
         self.http_version) \
         + self.delimiter)

    outgoing_proxy.firstline_sent_def.addCallback(
        self.outgoingFirstlineReceived)

    # Send anything we have queued.
    self.flushOutgoingBuffer()

    # Add callbacks for when headers are ready
    outgoing_proxy.headers_finished_def.addCallback(
        self.outgoingHeadersReceived)
    outgoing_proxy.request_finished_def.addCallback(
        self.handleOutgoingRequestFinished)

