def getPage(url, bindAddress = None, *arg, **kw): # reimplemented here to insert bindAddress # _parse() deprecated in twisted 13.1.0 in favor of the _URI class if hasattr(client, '_parse'): scheme, host, port, path = client._parse(url) else: # _URI class renamed to URI in 15.0.0 try: from twisted.web.client import _URI as URI except ImportError: from twisted.web.client import URI uri = URI.fromBytes(url) scheme = uri.scheme host = uri.host port = uri.port path = uri.path factory = HTTPClientFactory(url, *arg, **kw) factory.noisy = False if scheme == 'https': from twisted.internet import ssl context = ssl.ClientContextFactory() reactor.connectSSL(host, port, factory, context, bindAddress = bindAddress) else: reactor.connectTCP(host, port, factory, bindAddress = bindAddress) return factory.deferred
def getPage(url, bindAddress=None, *arg, **kw): # reimplemented here to insert bindAddress # _parse() deprecated in twisted 13.1.0 in favor of the _URI class if hasattr(client, '_parse'): scheme, host, port, path = client._parse(url) else: # _URI class renamed to URI in 15.0.0 try: from twisted.web.client import _URI as URI except ImportError: from twisted.web.client import URI uri = URI.fromBytes(url) scheme = uri.scheme host = uri.host port = uri.port path = uri.path factory = HTTPClientFactory(url, *arg, **kw) factory.noisy = False if scheme == 'https': from twisted.internet import ssl context = ssl.ClientContextFactory() reactor.connectSSL(host, port, factory, context, bindAddress=bindAddress) else: reactor.connectTCP(host, port, factory, bindAddress=bindAddress) return factory.deferred
def send_json(url, **kw): qurl =url+'?'+urlencode(kw.get('postdata', {})) # debug(0,'URLSTATS: %s',qurl) factory = HTTPClientFactory(str(qurl)) factory.noisy = False if url.startswith('http://'): reactor.connectTCP(factory.host, factory.port, factory) elif url.startswith('https://'): reactor.connectSSL(factory.host, factory.port, factory, ssl.ClientContextFactory()) else: raise Exception('Url error: %s' %url) return factory
def flush(self): scheme, host, port, path = _parse(self.url) factory = HTTPClientFactory(self.url, method='POST', postdata=self._buffer.getvalue()) factory.noisy = False if scheme == 'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL(host, port, factory, contextFactory) else: reactor.connectTCP(host, port, factory) factory.deferred.addCallbacks(self._handle, self._error) self._buffer = StringIO()
def _start(self): self.saved['Request URL'] = self.request_url # Generate a request id if possible if uuid: request_id = str(uuid.uuid1()) self.saved['Request ID'] = request_id self.headers['X-Request-Id'] = request_id factory = HTTPClientFactory(url=self.conf['path'], method=self.conf['method'], postdata=self.conf['data'], headers=self.headers, agent=self.agent, timeout=self.conf['timeout'], followRedirect=0) factory.host = self.headers_host factory.noisy = False factory.deferred.addErrback(self._failure_tcp) factory.deferred.addErrback(self._failure_http) self._connect(factory) return factory.deferred
def get_json(url, *args, **kwargs): """ :param json: JSON data :param url: :param args: :param kwargs: :return: """ j = kwargs.pop('json', None) if j: kwargs['postdata'] = as_json(j) kwargs.setdefault('agent', 'Twisted JSON Adapter') uri = URI.fromBytes(url) factory = HTTPClientFactory(url, *args, **kwargs) factory.noisy = 0 if uri.scheme == b'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL(nativeString(uri.host), uri.port, factory, contextFactory) else: reactor.connectTCP(nativeString(uri.host), uri.port, factory) return factory.deferred.addCallback(_json_loads)
def get_json(url, *args, **kwargs): """ :param json: JSON data :param url: :param args: :param kwargs: :return: """ j = kwargs.pop('json', None) if j: kwargs['postdata'] = as_json(j) kwargs.setdefault('agent', 'Twisted JSON Adapter') uri = URI.fromBytes(url) factory = HTTPClientFactory(url, *args, **kwargs) factory.noisy = 0 if uri.scheme == b'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL( nativeString(uri.host), uri.port, factory, contextFactory) else: reactor.connectTCP(nativeString(uri.host), uri.port, factory) return factory.deferred.addCallback(_json_loads)
def getPage(url, bindAddress=None, *arg, **kw): # reimplemented here to insert bindAddress uri = URI.fromBytes(url) scheme = uri.scheme host = uri.host port = uri.port path = uri.path factory = HTTPClientFactory(url, *arg, **kw) factory.noisy = False if scheme == 'https': from twisted.internet import ssl context = ssl.ClientContextFactory() reactor.connectSSL(host, port, factory, context, bindAddress=bindAddress) else: reactor.connectTCP(host, port, factory, bindAddress=bindAddress) return factory.deferred