Beispiel #1
0
 def _connect(self, local_uri, remote_uri, use_acm=False):
     self.logger.info('Connecting to %s' % (remote_uri, ))
     creator = GreenClientCreator(gtransport_class=MSRPTransport, local_uri=local_uri, logger=self.logger, use_acm=use_acm)
     connectFuncName = 'connect' + remote_uri.protocol_name
     connectFuncArgs = remote_uri.protocolArgs
     if remote_uri.host:
         args = (remote_uri.host, remote_uri.port or 2855) + connectFuncArgs
         msrp = getattr(creator, connectFuncName)(*args)
     else:
         if not remote_uri.domain:
             raise ValueError("remote_uri must have either 'host' or 'domain'")
         msrp = creator.connectSRV(remote_uri.scheme, remote_uri.domain,
                                   connectFuncName=connectFuncName,
                                   connectFuncArgs=connectFuncArgs,
                                   ConnectorClass=self.SRVConnectorClass)
     self.logger.info('Connected to %s:%s' % (msrp.getPeer().host, msrp.getPeer().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
from eventlet.twistedutil.protocol import GreenClientCreator
from eventlet.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:'
from twisted.internet import reactor
from twisted.names.srvconnect import SRVConnector
from gnutls.interfaces.twisted import X509Credentials

from eventlet.twistedutil.protocol import GreenClientCreator
from eventlet.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)
creator = GreenClientCreator(reactor, LineOnlyReceiverTransport)
conn = creator.connectSRV('msrps', 'ag-projects.com',
                          connectFuncName='connectTLS', connectFuncArgs=(cred,),
                          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))
Beispiel #5
0
"""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 eventlet.twistedutil.protocol import GreenClientCreator
from eventlet.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
"""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 eventlet.twistedutil.protocol import GreenClientCreator
from eventlet.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 as ex:
    print(ex)