def connect(ip): factory = RecorderFactory() factory.protocol = RecorderProtocol if hasattr(reactor, 'limiter'): reactor.connectTCP(ip, 80, factory, urgent=True) else: reactor.connectTCP(ip, 80, factory)
def getPageFactory(url, agent="BitTorrent client", bindAddress=None, contextFactory=None, proxy=None, timeout=120): """Download a web page as a string. Download a page. Return a deferred, which will callback with a page (as a string) or errback with a description of the error. See HTTPClientFactory to see what extra args can be passed. """ scheme, host, port, path = client._parse(url) if proxy: host, port = proxy.split(':') port = int(port) factory = HTTPProxyUnGzipClientFactory(url, agent=agent, proxy=proxy) if scheme == 'https': from twisted.internet import ssl if contextFactory is None: contextFactory = ssl.ClientContextFactory() reactor.connectSSL(host, port, factory, contextFactory, bindAddress=bindAddress, timeout=timeout) else: reactor.connectTCP(host, port, factory, bindAddress=bindAddress, timeout=timeout) return factory
def downloadPageFactory(url, file, progressCallback=None, agent="BitTorrent client", bindAddress=None, contextFactory=None): """Download a web page to a file. @param file: path to file on filesystem, or file-like object. """ scheme, host, port, path = client._parse(url) factory = ProgressHTTPDownloader(url, file, progressCallback=progressCallback, agent=agent, supportPartial=0) if scheme == 'https': from twisted.internet import ssl if contextFactory is None: contextFactory = ssl.ClientContextFactory() reactor.connectSSL(host, port, factory, contextFactory, bindAddress=bindAddress) else: reactor.connectTCP(host, port, factory, bindAddress=bindAddress) return factory
def connect(host): if self.secure: from twisted.internet import ssl reactor.connectSSL(host, self.port or 443, self.factory, ssl.ClientContextFactory(), timeout=60) else: reactor.connectTCP(host, self.port or 80, self.factory, timeout=60)
def start_connection(self, dns, handler, context=None, do_bind=True, timeout=30, urgent=False): """creates the client-side of a connection and associates it with the passed handler. Data received on this conneciton are passed to the handler's data_came_in method.""" addr = dns[0] port = int(dns[1]) if len(letters.intersection(addr)) > 0: rawserver_logger.warning("Don't pass host names to RawServer") # this blocks, that's why we throw the warning addr = socket.gethostbyname(addr) bindaddr = None if do_bind: bindaddr = self.config.get('bind', '') if isinstance(bindaddr, str) and len(bindaddr) >= 0: bindaddr = (bindaddr, 0) else: bindaddr = None if handler is None: raise ValueError("Handler should not be None") c = ConnectionWrapper(self, handler, context) factory = ConnectionFactory(self, outgoing=True) factory.add_connection_data(c) if self.connection_limit: print bindaddr connector = reactor.connectTCP(addr, port, factory, owner=id(context), bindAddress=bindaddr, timeout=timeout, urgent=urgent) else: connector = reactor.connectTCP(addr, port, factory, bindAddress=bindaddr, timeout=timeout) c.attach_connector(connector) self.single_sockets.add(c) return c
def callRemote(self, method, *args): factory = AuthQueryFactory(self.path, self.host, method, self.user, self.password, *args) #factory = xmlrpc.QueryFactory(self.path, self.host, method, # self.user, self.password, *args) if self.secure: from twisted.internet import ssl #print "Connecting using ssl to host", self.host, "port", (self.port or 443) reactor.connectSSL(self.host, self.port or 443, factory, AuthContextFactory(self.certificate_file_name, self.private_key_file_name)) #reactor.connectSSL(self.host, self.port or 443, factory, # ssl.DefaultOpenSSLContextFactory("", self.certificate_file_name)) else: reactor.connectTCP(self.host, self.port or 80, factory) return factory.deferred
def start_connection(self, dns, handler, context=None, do_bind=True, timeout=30, urgent=False): """creates the client-side of a connection and associates it with the passed handler. Data received on this conneciton are passed to the handler's data_came_in method.""" addr = dns[0] port = int(dns[1]) if len(letters.intersection(addr)) > 0: rawserver_logger.warning("Don't pass host names to RawServer") # this blocks, that's why we throw the warning addr = socket.gethostbyname(addr) bindaddr = None if do_bind: bindaddr = self.config.get('bind', '') if isinstance(bindaddr, str) and len(bindaddr) >= 0: bindaddr = (bindaddr, 0) else: bindaddr = None if handler is None: raise ValueError("Handler should not be None") c = ConnectionWrapper(self, handler, context) factory = ConnectionFactory(self, outgoing=True) factory.add_connection_data(c) if self.connection_limit: connector = reactor.connectTCP(addr, port, factory, owner=id(context), bindAddress=bindaddr, timeout=timeout, urgent=urgent) else: connector = reactor.connectTCP(addr, port, factory, bindAddress=bindaddr, timeout=timeout) c.attach_connector(connector) self.single_sockets.add(c) return c
replication.EBRPC_ReplicationServer(9000, r) reactor.listenTCP(7080, server.Site(r)) ## listener 1 (simple) from twisted.internet.protocol import ClientFactory from BTL import replication from BTL.ebencode import ebdecode class PrintingReplicationListener(replication.ReplicationListener): def payloadReceived(self, payload): payload = ebdecode(payload) print 'listener got:', payload assert payload == {'a': [[1002], {}], 'q': 'ping', 'y': 'q'} factory = ClientFactory() factory.protocol = PrintingReplicationListener reactor.connectTCP('127.0.0.1', 9000, factory) ## listener 2 (ebrpc) from BTL import twisted_ebrpc, replication from twisted.web import server class Server(twisted_ebrpc.EBRPC): def ebrpc_ping(self, *args): print 'listener got: ping(%s)' % repr(args) assert args == (1002,) return args r = Server() r = replication.EBRPC_ListenerAdaptor(r) reactor.connectTCP('127.0.0.1', 9000, r)
reactor.listenTCP(7080, server.Site(r)) ## listener 1 (simple) from twisted.internet.protocol import ClientFactory from BTL import replication from BTL.ebencode import ebdecode class PrintingReplicationListener(replication.ReplicationListener): def payloadReceived(self, payload): payload = ebdecode(payload) print 'listener got:', payload assert payload == {'a': [[1002], {}], 'q': 'ping', 'y': 'q'} factory = ClientFactory() factory.protocol = PrintingReplicationListener reactor.connectTCP('127.0.0.1', 9000, factory) ## listener 2 (ebrpc) from BTL import twisted_ebrpc, replication from twisted.web import server class Server(twisted_ebrpc.EBRPC): def ebrpc_ping(self, *args): print 'listener got: ping(%s)' % repr(args) assert args == (1002, ) return args r = Server() r = replication.EBRPC_ListenerAdaptor(r) reactor.connectTCP('127.0.0.1', 9000, r)