def _connect(self, local_uri, remote_uri): self.logger.info('Connecting to %s', remote_uri) creator = GreenClientCreator(gtransport_class=MSRPTransport, local_uri=local_uri, logger=self.logger, use_sessmatch=self.use_sessmatch) if remote_uri.host: if remote_uri.use_tls: msrp = creator.connectTLS(remote_uri.host, remote_uri.port or 2855, TLSContext(local_uri.credentials)) else: msrp = creator.connectTCP(remote_uri.host, remote_uri.port or 2855) else: if not remote_uri.domain: raise ValueError( "remote_uri must have either 'host' or 'domain'") if remote_uri.use_tls: connectFuncName = 'connectTLS' connectFuncArgs = (TLSContext(local_uri.credentials), ) else: connectFuncName = 'connectTCP' connectFuncArgs = () msrp = creator.connectSRV(remote_uri.scheme, remote_uri.domain, connectFuncName=connectFuncName, connectFuncArgs=connectFuncArgs, ConnectorClass=self.SRVConnectorClass) remote_address = msrp.getPeer() self.logger.info('Connected to %s:%s', remote_address.host, remote_address.port) return msrp
def handler(local): client = str(local.getHost()) print 'accepted connection from %s' % client remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP( remote_host, remote_port) a = proc.spawn(forward, remote, local) b = proc.spawn(forward, local, remote) proc.waitall([a, b], trap_errors=True) print 'closed connection to %s' % client
"""Example for GreenTransport and GreenClientCreator. In this example reactor is started implicitly upon the first use of a blocking function. """ from twisted.internet import ssl from twisted.internet.error import ConnectionClosed from eventlib.twistedutil.protocol import GreenClientCreator from eventlib.twistedutil.protocols.basic import LineOnlyReceiverTransport from twisted.internet import reactor print("\n\nRead from TCP connection\n\n") # read from TCP connection conn = GreenClientCreator(reactor).connectTCP('www.google.com', 80) conn.write('GET /not_found HTTP/1.0\r\n\r\n') conn.loseWriteConnection() print(conn.read().decode('utf-8')) print("\n\nRead from SSL connection line by line\n\n") # read from SSL connection line by line conn = GreenClientCreator(reactor, LineOnlyReceiverTransport).connectSSL( 'ssltest.com', 443, ssl.ClientContextFactory()) conn.write('GET /not_found HTTP/1.0\r\n\r\n') try: for num, line in enumerate(conn): print('%3s %r' % (num, line)) except ConnectionClosed as ex: print(ex)
"""Example for GreenTransport and GreenClientCreator. In this example reactor is started implicitly upon the first use of a blocking function. """ from twisted.internet import ssl from twisted.internet.error import ConnectionClosed from eventlib.twistedutil.protocol import GreenClientCreator from eventlib.twistedutil.protocols.basic import LineOnlyReceiverTransport from twisted.internet import reactor # read from TCP connection conn = GreenClientCreator(reactor).connectTCP('www.google.com', 80) conn.write('GET / HTTP/1.0\r\n\r\n') conn.loseWriteConnection() print conn.read() # read from SSL connection line by line conn = GreenClientCreator(reactor, LineOnlyReceiverTransport).connectSSL( 'sf.net', 443, ssl.ClientContextFactory()) conn.write('GET / HTTP/1.0\r\n\r\n') try: for num, line in enumerate(conn): print '%3s %r' % (num, line) except ConnectionClosed, ex: print ex
from twisted.names.srvconnect import SRVConnector from gnutls.interfaces.twisted import TLSContext, X509Credentials from eventlib.twistedutil.protocol import GreenClientCreator from eventlib.twistedutil.protocols.basic import LineOnlyReceiverTransport class NoisySRVConnector(SRVConnector): def pickServer(self): host, port = SRVConnector.pickServer(self) print('Resolved _%s._%s.%s --> %s:%s' % (self.service, self.protocol, self.domain, host, port)) return host, port cred = X509Credentials(None, None) ctx = TLSContext(cred) creator = GreenClientCreator(reactor, LineOnlyReceiverTransport) conn = creator.connectSRV('msrps', 'ag-projects.com', connectFuncName='connectTLS', connectFuncArgs=(ctx,), ConnectorClass=NoisySRVConnector) request = """MSRP 49fh AUTH To-Path: msrps://[email protected];tcp From-Path: msrps://alice.example.com:9892/98cjs;tcp -------49fh$ """.replace('\n', '\r\n') print('Sending:\n%s' % request) conn.write(request) print('Received:') for x in conn: print(repr(x))