Here are the examples of the python api twisted.internet.reactor.connectSSL taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
52 Examples
3
Example 1
Project: flud Source File: FludCommUtil.py
def _dlPageFactory(url, target, factoryClass, contextFactory=None, timeout=None,
*args, **kwargs):
scheme, host, port, path = client._parse(url)
if timeout != None:
# XXX: do something like http://twistedmatrix.com/pipermail/twisted-python/2003-August/005504.html
pass
factory = factoryClass(url, target, *args, **kwargs)
to = CONNECT_TO+random.randrange(2+CONNECT_TO_VAR)-CONNECT_TO_VAR
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory)
else:
reactor.connectTCP(host, port, factory, timeout=to)
return factory
3
Example 2
def connectToServer(self):
# Don't read anything from the connecting client until we have
# somewhere to send it to.
self.transport.pauseProducing()
clientFactory = self.getClientFactory()
host = random.choice(self.factory.hosts)
self.log('Connecting to push server: %s:%d' %
(host, self.factory.port))
reactor.connectSSL(host,
self.factory.port,
clientFactory,
self.getClientContextFactory())
3
Example 3
def _connect(self, factory):
host, port = to_unicode(factory.host), factory.port
if factory.scheme == b'https':
return reactor.connectSSL(host, port, factory,
self.ClientContextFactory())
else:
return reactor.connectTCP(host, port, factory)
3
Example 4
Project: punjab Source File: httpb_client.py
def connect(self, b):
"""
Make a connection to the web server and send along the data.
"""
self.factory = QueryFactory(self.url, self.host, b)
if self.secure:
from twisted.internet import ssl
self.rid = reactor.connectSSL(self.host,
self.port or 443,
self.factory,
ssl.ClientContextFactory())
else:
self.rid = reactor.connectTCP(self.host,
self.port or 80, self.factory)
return self.factory.deferred
3
Example 5
def _sendmail(self, to_addrs, msg):
msg = StringIO(msg)
d = defer.Deferred()
factory = ESMTPSenderFactory(self.smtpuser, self.smtppass, self.mailfrom, to_addrs, msg, d, heloFallback=True,
requireAuthentication=False,
requireTransportSecurity=self.smtptls)
factory.noisy = False
if self.smtpssl:
reactor.connectSSL(self.smtphost, self.smtpport, factory, ssl.ClientContextFactory())
else:
reactor.connectTCP(self.smtphost, self.smtpport, factory)
return d
3
Example 6
def _sendmail(self, to_addrs, msg):
# Import twisted.mail here because it is not available in python3
from twisted.mail.smtp import ESMTPSenderFactory
msg = StringIO(msg)
d = defer.Deferred()
factory = ESMTPSenderFactory(self.smtpuser, self.smtppass, self.mailfrom, \
to_addrs, msg, d, heloFallback=True, requireAuthentication=False, \
requireTransportSecurity=self.smtptls)
factory.noisy = False
if self.smtpssl:
reactor.connectSSL(self.smtphost, self.smtpport, factory, ssl.ClientContextFactory())
else:
reactor.connectTCP(self.smtphost, self.smtpport, factory)
return d
3
Example 7
def setServiceParent(self, service):
if self.ssl:
sslctx = ssl.ClientContextFactory()
if service:
internet.SSLClient(self.server, self.port, self, sslctx).setServiceParent(service)
else:
reactor.connectSSL(self.server, self.port, self, sslctx)
else:
if service:
internet.TCPClient(self.server, self.port, self).setServiceParent(service)
else:
reactor.connectTCP(self.server, self.port, self)
3
Example 8
def connectSSL(self, server, port, vhost):
"""Connect to the server using an SSL socket."""
drivers.log.info('Attempting an SSL connection.')
if SSL:
reactor.connectSSL(server, port, self,
ssl.ClientContextFactory(), bindAddress=(vhost, 0))
else:
drivers.log.error('PyOpenSSL is not available. Not connecting.')
3
Example 9
def _connect(self, factory):
host, port = factory.host, factory.port
if factory.scheme == 'https':
return reactor.connectSSL(host, port, factory,
self.ClientContextFactory())
else:
return reactor.connectTCP(host, port, factory)
3
Example 10
Project: hamper Source File: commander.py
def main():
config = hamper.config.load()
hamper.log.setup_logging()
if config.get('ssl', False):
reactor.connectSSL(
config['server'], config['port'], CommanderFactory(config),
ssl.ClientContextFactory())
else:
reactor.connectTCP(
config['server'], config['port'], CommanderFactory(config))
reactor.run()
3
Example 11
def connect(self):
networking = self.config["network"]
context = self._get_client_context()
if context is None:
return False
reactor.connectSSL(
networking["address"],
networking["port"],
self,
context,
120
)
return True
3
Example 12
Project: flumotion Source File: test_component_icystreamer.py
def downloadStream(url, contextFactory=None, *args, **kwargs):
scheme, host, port, path = client._parse(url)
factory = StreamDownloader(url, *args, **kwargs)
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory)
else:
reactor.connectTCP(host, port, factory)
return factory.deferred
3
Example 13
Project: filesync-server Source File: cmd_client.py
@parse_args(str, int)
def do_connect_ssl(self, host, port):
"""Connect to host/port using ssl."""
def _connect():
"""deferred part."""
reactor.connectSSL(host, port, self.factory,
ssl.ClientContextFactory())
self.status = "connecting"
reactor.callFromThread(_connect)
3
Example 14
def join_room(self, room_id):
log.debug('Joining room: %i', room_id)
self._streams[room_id] = stream = JSONStream(
'https://streaming.campfirenow.com/room/%i/live.json' % room_id,
keepalive_timeout=self.keepalive_timeout,
headers={'Authorization': self._auth_header()})
stream.event = self._event
stream.stream_connected = lambda : self._joined_room(room_id)
stream.clientConnectionLost = lambda connector, unused_reason: \
self.stream_failure(connector, unused_reason, room_id)
contextFactory = ssl.ClientContextFactory()
stream.proto = reactor.connectSSL(
'streaming.campfirenow.com', 443,
stream, contextFactory)
3
Example 15
def start(self):
"""
Connect session to sessionhandler.
"""
if self.port:
if self.ssl:
try:
from twisted.internet import ssl
service = reactor.connectSSL(self.network, int(self.port), self, ssl.ClientContextFactory())
except ImportError:
self.caller.msg("To use SSL, the PyOpenSSL module must be installed.")
else:
service = internet.TCPClient(self.network, int(self.port), self)
self.sessionhandler.portal.services.addService(service)
3
Example 16
def callRemote(self, method, *args, **kwargs):
version = self._getVersion(kwargs)
# XXX generate unique id and pass it as a parameter
factoryClass = self._getFactoryClass(kwargs)
factory = factoryClass(self.path, self.host, method, self.user,
self.password, version, *args)
if self.secure:
from twisted.internet import ssl
if self.ssl_ctx_factory is None:
self.ssl_ctx_factory = ssl.ClientContextFactory
reactor.connectSSL(self.host, self.port or 443,
factory, self.ssl_ctx_factory())
else:
reactor.connectTCP(self.host, self.port or 80, factory)
return factory.deferred
3
Example 17
Project: gazouilleur Source File: httpget.py
def conditionalGetPage(cacheDir, url, contextFactory=None, timeout=0, *args, **kwargs):
scheme, host, port, _ = parse_url(url)
factory = ConditionalHTTPClientFactory(cacheDir, url, timeout=timeout, *args, **kwargs)
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory, timeout=timeout)
else:
reactor.connectTCP(host, port, factory, timeout=timeout)
return factory.deferred
3
Example 18
def getPage(url, contextFactory=None, *args, **kwargs):
"""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)
factory = client.HTTPClientFactory(url, *args, **kwargs)
if scheme == 'https':
if contextFactory is None:
raise RuntimeError, 'must provide a contextFactory'
conn = reactor.connectSSL(host, port, factory, contextFactory)
else:
conn = reactor.connectTCP(host, port, factory)
return factory
3
Example 19
def startConnecting(self, connectionInfo):
info = connectionInfo
self.factory = WorkerClientFactory(self, info.host, info.port)
self.factory.startLogin(info.authenticator)
if info.use_ssl:
from flumotion.common import common
common.assertSSLAvailable()
from twisted.internet import ssl
reactor.connectSSL(info.host, info.port, self.factory,
ssl.ClientContextFactory())
else:
reactor.connectTCP(info.host, info.port, self.factory)
0
Example 20
def __downloadPage(factory, *args, **kwargs):
"""Start a HTTP download, returning a HTTPDownloader object"""
# The Twisted API is weird:
# 1) web.client.downloadPage() doesn't give us the HTTP headers
# 2) there is no method that simply accepts a URL and gives you back
# a HTTPDownloader object
#TODO: convert getPage() usage to something similar, too
downloader = factory(*args, **kwargs)
if downloader.scheme == 'https':
from twisted.internet import ssl
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(downloader.host, downloader.port,
downloader, contextFactory)
else:
reactor.connectTCP(downloader.host, downloader.port,
downloader)
return downloader
0
Example 21
Project: flud Source File: FludCommUtil.py
def getPageFactory(url, contextFactory=None, *args, **kwargs):
def failedConnect(reason, factory):
try:
i = factory.status
return reason
except:
pass
#logger.warn("couldn't connect to %s:%d in getPageFactory: %s"
# % (factory.host, factory.port, reason))
#logger.warn("state of factory is %s" % factory)
#logger.warn("dir() of factory is %s" % dir(factory))
return reason
if len(url) >= 16384:
raise ValueError(
"Too much data sent: twisted server doesn't appear to"
" support urls longer than 16384")
scheme, host, port, path = client._parse(url)
factory = client.HTTPClientFactory(url, *args, **kwargs)
factory.deferred.addErrback(failedConnect, factory)
to = CONNECT_TO+random.randrange(2+CONNECT_TO_VAR)-CONNECT_TO_VAR
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory)
else:
reactor.connectTCP(host, port, factory, timeout=to)
return factory
0
Example 22
Project: Piped Source File: smtp_provider.py
def send_mail(from_addr, to_addr, file, host='localhost', port=25, timeout=60, retries=5,
use_ssl=False, require_transport_security=None,
require_authentication=None, username=None, password=None,
helo_fallback=False):
""" A utility function used to send email.
:param from_addr: Sender email address.
:param to_addr: Recipients email address.
:param file: The full message, including headers. Any file-like or any object with
a ``__str__`` can be used.
:param host: Which SMTP server to use.
:param port: Which port to connect to.
:param use_ssl: Whether the server speaks SSL.
:param require_transport_security: Whether to require transport security (TLS) before
sending the email.
Defaults to ``True`` if a username is specified.
Defaults to ``False`` if using SSL due to `Twisted #3989
<http://twistedmatrix.com/trac/ticket/3989>`_.
:param require_authentication: Whether to login before sending an email.
Defaults to ``True`` if a username is specified, ``False`` otherwise.
:param username: The username used when logging in.
:param password: The password used when logging in.
:param timeout: Number of seconds to wait before timing out a connection. If
``None``, perform no timeout checking.
:param retries: Number of retries if we cannot connect to the server.
:param helo_fallback: Whether to fallback to HELO if EHLO fails.
"""
if hasattr(file, 'as_string'):
# this looks like a email.message.Message
file = StringIO(file.as_string())
if not hasattr(file, 'read'):
file = StringIO(str(file))
if require_transport_security is None:
if use_ssl:
require_transport_security = False
else:
require_transport_security = username is not None
if require_authentication is None:
require_authentication = username is not None
context_factory = None
if use_ssl or require_transport_security:
# only import the ssl module if required
from twisted.internet import ssl
context_factory = ssl.ClientContextFactory()
deferred = defer.Deferred()
sender_factory = smtp.ESMTPSenderFactory(
contextFactory = context_factory,
deferred = deferred,
timeout = timeout,
fromEmail = from_addr,
toEmail = to_addr,
file = file,
retries = retries,
requireTransportSecurity = require_transport_security,
requireAuthentication = require_authentication,
username = username,
password = password,
heloFallback = helo_fallback,
)
if use_ssl:
reactor.connectSSL(host, port, sender_factory, context_factory)
else:
reactor.connectTCP(host, port, sender_factory)
return deferred
0
Example 23
Project: idavoll Source File: gateway.py
def getPageWithFactory(url, contextFactory=None, *args, **kwargs):
"""Download a web page.
Download a page. Return the factory that holds 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.
"""
factory = client.HTTPClientFactory(url, *args, **kwargs)
factory.protocol.handleStatus_204 = lambda self: self.handleStatus_200()
if factory.scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(factory.host, factory.port, factory, contextFactory)
else:
reactor.connectTCP(factory.host, factory.port, factory)
return factory
0
Example 24
Project: Tor2web Source File: lists.py
def getPageCached(url, contextFactory=None, *args, **kwargs):
"""download a web page as a string, keep a cache of already downloaded pages
Download a page. Return a deferred, which will callback with a
page (as a string) or errback with a description of the error.
See HTTPClientCacheFactory to see what extra args can be passed.
"""
uri = _URI.fromBytes(url)
scheme = uri.scheme
host = uri.host
port = uri.port
factory = HTTPClientCacheFactory(url, *args, **kwargs)
if scheme == 'https':
if contextFactory is None:
contextFactory = HTTPSVerifyingContextFactory(host)
reactor.connectSSL(host, port, factory, contextFactory)
else:
reactor.connectTCP(host, port, factory)
return factory.deferred
0
Example 25
def setup(self):
self.players = []
self.factory = IRCBotFactory(self)
if self.ssl:
if have_ssl:
cf = Mark2ClientContextFactory(self,
cert=self.certificate,
fingerprint=self.server_fingerprint)
reactor.connectSSL(self.host, self.port, self.factory, cf)
else:
self.parent.console("Couldn't load SSL for IRC!")
return
else:
reactor.connectTCP(self.host, self.port, self.factory)
if self.game_status_enabled:
self.register(self.handle_stopping, ServerStopping)
self.register(self.handle_starting, ServerStarting)
self.column_width = 16
if self.cancel_highlight == "insert":
self.column_width += len(self.cancel_highlight_str)
def register(event_type, format, filter_=None, *a, **k):
def handler(event, format):
d = event.match.groupdict() if hasattr(event, 'match') else event.serialize()
if filter_ and 'message' in d:
if filter_.match(d['message']):
return
if self.cancel_highlight and 'username' in d and d['username'] in self.factory.client.users:
d['username'] = self.mangle_username(d['username'])
line = self.format(format, **d)
self.factory.irc_relay(line)
self.register(lambda e: handler(e, format), event_type, *a, **k)
if self.game_chat_enabled:
if self.game_chat_private:
try:
filter_ = re.compile(self.game_chat_private)
register(PlayerChat, self.game_chat_format, filter_=filter_)
except:
self.console("plugin.irc.game_chat_private must be a valid regex")
register(PlayerChat, self.game_chat_format)
else:
register(PlayerChat, self.game_chat_format)
if self.game_join_enabled:
register(PlayerJoin, self.game_join_format)
if self.game_quit_enabled:
register(PlayerQuit, self.game_quit_format)
if self.game_death_enabled:
def handler(event):
d = event.serialize()
for k in 'username', 'killer':
if k in d and d[k] and d[k] in self.factory.client.users:
d[k] = self.mangle_username(d[k])
text = event.get_text(**d)
line = self.format(self.game_death_format, text=text)
self.factory.irc_relay(line)
self.register(handler, PlayerDeath)
if self.game_server_message_enabled and not (self.irc_chat_enabled and self.irc_chat_command.startswith('say ')):
register(ServerOutput, self.game_server_message_format, pattern=r'\[(?:Server|SERVER)\] (?P<message>.+)')
if self.game_me_enabled:
register(ServerOutput, self.game_me_format, pattern=r'\* (?P<username>[A-Za-z0-9_]{1,16}) (?P<message>.+)')
if self.irc_chat_enabled:
self.register(self.handle_players, StatPlayers)
0
Example 26
Project: scrapy Source File: client.py
def _makeGetterFactory(url, factoryFactory, contextFactory=None,
*args, **kwargs):
"""
Create and connect an HTTP page getting factory.
Any additional positional or keyword arguments are used when calling
C{factoryFactory}.
@param factoryFactory: Factory factory that is called with C{url}, C{args}
and C{kwargs} to produce the getter
@param contextFactory: Context factory to use when creating a secure
connection, defaulting to C{None}
@return: The factory created by C{factoryFactory}
"""
scheme, host, port, path = _parse(url)
factory = factoryFactory(url, *args, **kwargs)
if scheme == b'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory)
else:
reactor.connectTCP(host, port, factory)
return factory
0
Example 27
Project: mark2 Source File: __init__.py
def jar_get(name):
d_result = defer.Deferred()
def got_data(factory, data):
filename = factory.path.split('/')[-1]
#parse the Content-Disposition header
dis = factory.response_headers.get('content-disposition', None)
if dis:
dis = dis[0].split(';')
if dis[0] == 'attachment':
for param in dis[1:]:
key, value = param.strip().split('=')
if key == 'filename':
filename = value.replace("\"", "")
d_result.callback((filename, data))
def got_results(results):
for r in results:
if name == '-'.join(r.name_short):
factory = HTTPClientFactory(r.url)
if factory.scheme == 'https':
if ssl:
reactor.connectSSL(factory.host, factory.port, factory, ssl.ClientContextFactory())
else:
d_result.errback(Exception("{0} is not available because this installation does not have SSL support!".format(name)))
else:
reactor.connectTCP(factory.host, factory.port, factory)
factory.deferred.addCallback(lambda d: got_data(factory, d))
factory.deferred.addErrback(d_result.errback)
return
d_result.errback(Exception("{0} is not available!".format(name)))
d = get_raw()
d.addCallbacks(got_results, d_result.errback)
return d_result
0
Example 28
def connect(self, host, port):
"""
Connects to a daemon at host:port
:param host: str, the host to connect to
:param port: int, the listening port on the daemon
:returns: twisted.Deferred
"""
log.debug('sslproxy.connect()')
self.host = host
self.port = port
self.__connector = reactor.connectSSL(self.host, self.port,
self.__factory,
ssl.ClientContextFactory())
self.connect_deferred = defer.Deferred()
self.daemon_info_deferred = defer.Deferred()
# Upon connect we do a 'daemon.login' RPC
self.connect_deferred.addCallback(self.__on_connect)
self.connect_deferred.addErrback(self.__on_connect_fail)
return self.daemon_info_deferred
0
Example 29
Project: txmongo Source File: connection.py
def __tcp_or_ssl_connect(self, host, port, factory, **kwargs):
if self.ssl_context_factory:
return reactor.connectSSL(host, port, factory, self.ssl_context_factory, **kwargs)
else:
return reactor.connectTCP(host, port, factory, **kwargs)
0
Example 30
def main():
sys.path.append(os.path.join(sys.path[0], 'lib'))
config = read_config()
# if config not found or can't validate it, exit with error
if not config or not validate_config(config):
sys.exit(1)
init_logging(config.get('logging', {}))
factory = PyFiBotFactory(config)
for network, settings in config['networks'].items():
# settings = per network, config = global
nick = settings.get('nick', None) or config['nick']
realname = settings.get('realname') or config.get('realname')
linerate = settings.get('linerate', 0.5) or config.get('linerate', 0.5)
password = settings.get('password', None)
is_ssl = bool(settings.get('is_ssl', False))
port = int(settings.get('port', 6667))
force_ipv6 = bool(settings.get('force_ipv6', False))
# normalize channel names to prevent internal confusion
chanlist = []
# allow bot to connect even if no channels are declared
if 'channels' in settings:
for channel in settings['channels']:
if channel[0] not in '&#!+':
channel = '#' + channel
chanlist.append(channel)
else:
log.warning('No channels defined for "%s"' % network)
if force_ipv6:
try:
server_name = socket.getaddrinfo(settings['server'], port, socket.AF_INET6)[0][4][0]
except IndexError:
log.error('No IPv6 address found for %s (force_ipv6 = true)' % (network))
continue
else:
server_name = settings['server']
factory.createNetwork((server_name, port), network, nick, realname, chanlist, linerate, password, is_ssl)
if is_ssl:
log.info("connecting via SSL to %s:%d" % (server_name, port))
reactor.connectSSL(server_name, port, factory, ssl.ClientContextFactory())
else:
log.info("connecting to %s:%d" % (server_name, port))
reactor.connectTCP(server_name, port, factory)
reactor.run()
0
Example 31
Project: downpour Source File: __init__.py
def serveNext(self):
with self.lock:
while self.numFlight < self.poolSize:
r = self.pop()
if r == None:
return
logger.debug('Requesting %s' % r.url)
self.numFlight += 1
try:
# This is the expansion of the short version getPage
# and is taken from twisted's source
scheme, host, port, path = parse(r.url)
factory = BaseRequestServicer(r, self.agent)
# If http_proxy or https_proxy, or whatever appropriate proxy
# is set, then we should try to honor that. We do so simply
# by overriding the host/port we'll connect to. The client
# factory, BaseRequestServicer takes care of the rest
proxy = os.environ.get('%s_proxy' % scheme) or r.proxy
if proxy:
scheme, host, port, path = parse(proxy)
if scheme == 'https':
from twisted.internet import ssl
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port or 443, factory, contextFactory)
else:
reactor.connectTCP(host, port or 80, factory)
factory.deferred.addCallback(r._success, self).addCallback(self._success)
factory.deferred.addErrback(r._error, self).addErrback(self._error).addErrback(log.err)
factory.deferred.addBoth(r._done, self).addBoth(self._done)
except:
self.numFlight -= 1
logger.exception('Unable to request %s' % r.url)
0
Example 32
Project: awspider Source File: requestqueuer.py
def _getPage(self, req):
scheme, host, port = _parse(req['url'])[0:3]
factory = HTTPClientFactory(
req['url'],
method=req['method'],
postdata=req['postdata'],
headers=req['headers'],
agent=req['agent'],
timeout=req['timeout'],
cookies=req['cookies'],
followRedirect=req['follow_redirect']
)
if scheme == 'https':
reactor.connectSSL(
host,
port,
factory,
AllCipherSSLClientContextFactory(),
timeout=req['timeout']
)
else:
reactor.connectTCP(host, port, factory, timeout=req['timeout'])
factory.deferred.addCallback(self._getPageComplete, factory)
factory.deferred.addErrback(self._getPageError, factory)
return factory.deferred
0
Example 33
def _connect(self, factory):
reactor.connectSSL(self.addr, self.conf['port'],
factory, self.context, self.conf['timeout'])
0
Example 34
def connect(self):
try:
from twisted.internet import ssl
use_ssl = True
except ImportError:
ssl = None
use_ssl = False
self.logger.exception(
"Unable to import the SSL library. SSL will not be available."
)
networking = self.config["network"]
binding = networking.get("bindaddr", None)
bindaddr = None
if binding:
bindaddr = (binding, 0)
if networking["ssl"] and not use_ssl:
self.logger.error(
"SSL is not available but was requested in the configuration."
)
self.logger.error(
"This protocol will be unavailable until SSL is fixed, or you "
"disable it in the configuration."
)
return False
if networking["ssl"]:
self.logger.debug("Connecting with SSL")
reactor.connectSSL(
networking["address"],
networking["port"],
self,
ssl.ClientContextFactory(),
120,
bindAddress=bindaddr
)
else:
self.logger.debug("Connecting without SSL")
reactor.connectTCP(
networking["address"],
networking["port"],
self,
120,
bindAddress=bindaddr
)
return True
0
Example 35
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
0
Example 36
Project: jeeves-framework Source File: __init__.py
def run_bot():
from jeeves.conf import settings
from jeeves.core.bot import BotFactory
factory = BotFactory(
channel=settings.CHANNEL,
nickname=settings.NICKNAME,
password=settings.PASSWORD
)
if settings.SSL == True:
if ssl:
reactor.connectSSL(
settings.HOST,
settings.PORT,
factory,
ssl.ClientContextFactory()
)
else:
print "Unable to establish an SSL connection."
else:
reactor.connectTCP(
settings.HOST,
settings.PORT,
factory
)
reactor.run()
0
Example 37
Project: ZenPacks.zenoss.OpenStackInfrastructure Source File: PerfCeilometerAPIDataSource.py
def get_page(self, headers={}, contextFactory=None, *args, **kwargs):
scheme, _, _, _ = txwebclient._parse(self.url)
factory = txwebclient.HTTPClientFactory(self.url)
for k, v in headers.iteritems():
factory.headers[k] = v.encode("utf-8")
try:
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
if self.use_proxy:
reactor.connectSSL(self.proxy_host, self.proxy_port,
factory, contextFactory)
else:
reactor.connectSSL(self.host, self.port,
factory, contextFactory)
else:
if self.use_proxy:
reactor.connectTCP(self.proxy_host, self.proxy_port, factory)
else:
reactor.connectTCP(self.host, self.port, factory)
except Exception, ex:
code = getattr(ex, 'status', None)
log.error('return code: %s, msg: %s', code, ex.message)
return factory.deferred.addCallbacks(self.getdata, self.errdata)
0
Example 38
def createClient(self):
"""Create a TCP connection to Riemann with automatic reconnection
"""
server = self.config.get('server', 'localhost')
port = self.config.get('port', 5555)
failover = self.config.get('failover', False)
self.factory = riemann.RiemannClientFactory(server, failover=failover)
if failover:
initial = random.choice(server)
else:
initial = server
log.msg('Connecting to Riemann on %s:%s' % (initial, port))
if self.tls:
if SSL:
self.connector = reactor.connectSSL(initial, port, self.factory,
ClientTLSContext(self.key, self.cert))
else:
log.msg('[FATAL] SSL support not available!' \
' Please install PyOpenSSL. Exiting now')
reactor.stop()
else:
self.connector = reactor.connectTCP(initial, port, self.factory)
d = defer.Deferred()
def cb():
# Wait until we have a useful proto object
if hasattr(self.factory, 'proto') and self.factory.proto:
self.t.start(self.inter)
d.callback(None)
else:
reactor.callLater(0.01, cb)
cb()
return d
0
Example 39
Project: leap_mail Source File: service.py
def _route_msg(self, encrypt_and_sign_result, raw):
"""
Sends the msg using the ESMTPSenderFactory.
:param encrypt_and_sign_result: A tuple containing the 'maybe'
encrypted message and the recipient
:type encrypt_and_sign_result: tuple
"""
message, recipient = encrypt_and_sign_result
log.msg("Connecting to SMTP server %s:%s" % (self._host, self._port))
msg = message.as_string(False)
# we construct a defer to pass to the ESMTPSenderFactory
d = defer.Deferred()
d.addCallback(self.sendSuccess)
d.addErrback(self.sendError, raw)
# we don't pass an ssl context factory to the ESMTPSenderFactory
# because ssl will be handled by reactor.connectSSL() below.
factory = smtp.ESMTPSenderFactory(
"", # username is blank, no client auth here
"", # password is blank, no client auth here
self._from_address,
recipient.dest.addrstr,
StringIO(msg),
d,
heloFallback=True,
requireAuthentication=False,
requireTransportSecurity=True)
factory.domain = bytes('leap.mail-' + __version__)
emit_async(catalog.SMTP_SEND_MESSAGE_START,
self._from_address, recipient.dest.addrstr)
reactor.connectSSL(
self._host, self._port, factory,
contextFactory=SSLContextFactory(self._cert, self._key))
0
Example 40
Project: awspider Source File: networkaddress.py
def getPage( url, method='GET', postdata=None, headers=None, agent="AWSpider", timeout=60, cookies=None, followRedirect=1 ):
scheme, host, port, path = _parse(url)
factory = HTTPClientFactory(
url,
method=method,
postdata=postdata,
headers=headers,
agent=agent,
timeout=timeout,
cookies=cookies,
followRedirect=followRedirect
)
if scheme == 'https':
from twisted.internet import ssl
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory, timeout=timeout)
else:
reactor.connectTCP(host, port, factory, timeout=timeout)
return factory.deferred
0
Example 41
def write(self, notifications):
"Connect to the APNS service and send notifications"
if not self.factory:
log.msg('APNSService write (connecting)')
server, port = ((APNS_SERVER_SANDBOX_HOSTNAME
if self.environment == 'sandbox'
else APNS_SERVER_HOSTNAME), APNS_SERVER_PORT)
self.factory = self.clientProtocolFactory()
context = self.getContextFactory()
reactor.connectSSL(server, port, self.factory, context)
client = self.factory.clientProtocol
if client:
return client.sendMessage(notifications)
else:
d = self.factory.deferred
timeout = reactor.callLater(self.timeout,
lambda: d.called or d.errback(
Exception('Notification timed out after %i seconds' % self.timeout)))
def cancel_timeout(r):
try: timeout.cancel()
except: pass
return r
d.addCallback(lambda p: p.sendMessage(notifications))
d.addErrback(log_errback('apns-service-write'))
d.addBoth(cancel_timeout)
return d
0
Example 42
Project: flumotion Source File: admin.py
def connectToManager(self, connectionInfo, keepTrying=False,
writeConnection=True):
"""
Connects to the specified manager.
@param connectionInfo: data for establishing the connection
@type connectionInfo: a L{PBConnectionInfo}
@param keepTrying: when this is L{True} the Factory will try to
reconnect when it loses the connection
@type keepTrying: bool
@param writeConnection: when this is L{True} the connection is saved
for future uses on cache
@type writeConnection: bool
@rtype: L{twisted.internet.defer.Deferred}
"""
assert self.clientFactory is None
self.connectionInfo = connectionInfo
self._writeConnection = writeConnection
# give the admin an id unique to the manager -- if a program is
# adminning multiple managers, this id should tell them apart
# (and identify duplicates)
self.managerId = str(connectionInfo)
self.logName = self.managerId
self.info('Connecting to manager %s with %s',
self.managerId, connectionInfo.use_ssl and 'SSL' or 'TCP')
self.clientFactory = AdminClientFactory(self,
extraTenacious=keepTrying,
maxDelay=20)
self.clientFactory.startLogin(connectionInfo.authenticator)
if connectionInfo.use_ssl:
common.assertSSLAvailable()
from twisted.internet import ssl
reactor.connectSSL(connectionInfo.host, connectionInfo.port,
self.clientFactory, ssl.ClientContextFactory())
else:
reactor.connectTCP(connectionInfo.host, connectionInfo.port,
self.clientFactory)
def connected(model, d):
# model is really "self". yay gobject?
d.callback(model)
def disconnected(model, d):
# can happen after setRemoteReference but before
# getPlanetState or getWorkerHeavenState returns
if not keepTrying:
d.errback(errors.ConnectionFailedError('Lost connection'))
def connection_refused(model, d):
if not keepTrying:
d.errback(errors.ConnectionRefusedError())
def connection_failed(model, reason, d):
if not keepTrying:
d.errback(errors.ConnectionFailedError(reason))
def connection_error(model, failure, d):
if not keepTrying:
d.errback(failure)
d = defer.Deferred()
ids = []
ids.append(self.connect('connected', connected, d))
ids.append(self.connect('disconnected', disconnected, d))
ids.append(self.connect('connection-refused', connection_refused, d))
ids.append(self.connect('connection-failed', connection_failed, d))
ids.append(self.connect('connection-error', connection_error, d))
def success(model):
map(self.disconnect, ids)
self._deferredConnect = None
return model
def failure(f):
map(self.disconnect, ids)
self._deferredConnect = None
return f
d.addCallbacks(success, failure)
self._deferredConnect = d
return d
0
Example 43
Project: conn-check Source File: checks.py
def make_smtp_check(host, port, username, password, from_address, to_address,
message='', subject='', helo_fallback=False, use_tls=True,
timeout=None, **kwargs):
"""Return a check for SMTP connectivity."""
subchecks = []
subchecks.append(make_tcp_check(host, port, timeout=timeout))
if use_tls:
subchecks.append(make_tls_check(host, port, verify=False,
timeout=timeout))
@inlineCallbacks
def do_connect():
"""Connect and authenticate."""
result_deferred = Deferred()
context_factory = None
if use_tls:
from twisted.internet import ssl as twisted_ssl
context_factory = twisted_ssl.ClientContextFactory()
body = MIMEText(message)
body['Subject'] = subject
factory = ESMTPSenderFactory(
username,
password,
from_address,
to_address,
StringIO(body.as_string()),
result_deferred,
contextFactory=context_factory,
requireTransportSecurity=use_tls,
requireAuthentication=True,
heloFallback=helo_fallback)
if use_tls:
reactor.connectSSL(host, port, factory, context_factory)
else:
reactor.connectTCP(host, port, factory)
result = yield result_deferred
if result[0] == 0:
raise RuntimeError("failed to send email via smtp")
subchecks.append(make_check("smtp:{}:{}".format(host, port),
do_connect, info="user {}".format(username),))
return sequential_check(subchecks)
0
Example 44
Project: Community-Zenpacks Source File: Adapter.py
def connect(self):
"""connect to the jabber server"""
self.logger.debug('Starting to connect')
self.logger.debug('Building context factory for jid %s' % self.myJid)
factory = client.basicClientFactory(jid.JID(self.myJid), self.password)
factory.addBootstrap('//event/stream/authd', self.authenticate)
self.logger.debug('Connecting to server %s (port %d) using id %s...' % (self.server, self.port, self.myJid))
if self.ssl:
class contextFactory:
isClient = 1
method = ssl.SSL.SSLv3_METHOD
def getContext(self):
context = ssl.SSL.Context(self.method)
return context
self.logger.debug('Connecting with ssl...')
reactor.connectSSL(self.server, self.port, factory, contextFactory())
else:
reactor.connectTCP(self.server, self.port, factory)
return reactor
0
Example 45
def callback_test(self, func, wait_notifications=0,
timeout=None, caps=PREFERRED_CAP,
add_default_callbacks=False, use_ssl=False, **kwargs):
"""Create a client and call callback on connection."""
if add_default_callbacks:
@wraps(func)
def wrapped(client, **kwargs):
"""Wrapper which wires up test_done/test_fail."""
d = func(client, **kwargs)
d.addCallbacks(client.test_done, client.test_fail)
return d
else:
wrapped = func
f = FactoryHelper(wrapped, factory=self.buildFactory(),
wait_notifications=wait_notifications,
timeout=timeout, caps=caps, **kwargs)
# there are 3 ways to connect to a server.
# tcp and ssl will work in the tests
if use_ssl:
reactor.connectSSL("localhost", self.ssl_port, f,
ssl.ClientContextFactory())
else:
reactor.connectTCP("localhost", self.port, f)
# https connect requires a working proxy and a server on
# the default port running (we are not setting this up for
# automated testing yet)
#proxy_tunnel.connectHTTPS('localhost', 3128, "localhost", 20101, f,
# user="test", passwd="test")
return f.test_deferred
0
Example 46
Project: hellanzb Source File: __init__.py
def connectServer(serverName, serverDict, defaultAntiIdle, defaultIdleTimeout):
""" Establish connections to the specified server according to the server information dict
(constructed from the config file). Returns the number of connections that were attempted
to be made """
defaultConnectTimeout = 30
connectionCount = 0
hosts = serverDict['hosts']
connections = int(serverDict['connections'])
for host in hosts:
antiIdle = int(setWithDefault(serverDict, 'antiIdle', defaultAntiIdle))
idleTimeout = int(setWithDefault(serverDict, 'idleTimeout', defaultIdleTimeout))
skipGroupCmd = setWithDefault(serverDict, 'skipGroupCmd', False)
fillServer = setWithDefault(serverDict, 'fillserver', 0)
useSSL = setWithDefault(serverDict, 'ssl', False)
nsf = NZBLeecherFactory(serverDict['username'], serverDict['password'],
idleTimeout, antiIdle, host, serverName, skipGroupCmd,
fillServer)
color = nsf.color
Hellanzb.nsfs.append(nsf)
split = host.split(':')
host = split[0]
if len(split) == 2:
port = int(split[1])
else:
port = 119
nsf.host, nsf.port = host, port
preWrappedNsf = nsf
nsf = HellaThrottlingFactory(nsf)
ctxf = None
if useSSL:
try:
from twisted.internet.ssl import Connector as SSLConnector
from twisted.internet.ssl import ClientContextFactory
except ImportError, ie:
error('Unable to use SSL for server: %s\npyOpenSSL is not '
'installed: %s' % (serverName, str(ie)))
shutdownAndExit(1)
ctxf = ClientContextFactory()
for connection in range(connections):
if serverDict.has_key('bindTo') and serverDict['bindTo'] != None and \
serverDict['bindTo'] != '':
if antiIdle != 0:
if useSSL:
reactor.connectSSL(host, port, nsf, ctxf,
bindAddress = (serverDict['bindTo'], 0))
else:
reactor.connectTCP(host, port, nsf,
bindAddress = (serverDict['bindTo'], 0))
else:
if useSSL:
connector = SSLConnector(host, port, nsf, ctxf,
defaultConnectTimeout,
(serverDict['bindTo'], 0), reactor=reactor)
else:
connector = Connector(host, port, nsf, defaultConnectTimeout,
(serverDict['bindTo'], 0), reactor=reactor)
else:
if antiIdle != 0:
if useSSL:
reactor.connectSSL(host, port, nsf, ctxf)
else:
reactor.connectTCP(host, port, nsf)
else:
if useSSL:
connector = SSLConnector(host, port, nsf, ctxf,
defaultConnectTimeout, None,
reactor=reactor)
else:
connector = Connector(host, port, nsf,
defaultConnectTimeout, None,
reactor=reactor)
if antiIdle == 0:
preWrappedNsf.leecherConnectors.append(connector)
connectionCount += 1
preWrappedNsf.setConnectionCount(connectionCount)
if antiIdle == 0:
action = ''
else:
action = 'Opening '
fillServerStatus = ''
if isinstance(Hellanzb.queue, FillServerQueue):
fillServerStatus = '[fillserver: %i] ' % preWrappedNsf.fillServerPriority
msg = preWrappedNsf.color + '(' + serverName + ') ' + Hellanzb.ACODE.RESET + \
fillServerStatus + action + str(connectionCount)
logFileMsg = '(' + serverName + ') ' + fillServerStatus + 'Opening ' + \
str(connectionCount)
if connectionCount == 1:
suffix = ' connection'
else:
suffix = ' connections'
msg += suffix
logFileMsg += suffix
if antiIdle != 0:
msg += '...'
logFileMsg += '...'
logFile(logFileMsg)
noLogFile(msg)
# HACK: remove this as a recentLog entry -- replace it with the version without color
# codes
Hellanzb.recentLogs.logEntries.pop()
Hellanzb.recentLogs.append(logging.INFO, logFileMsg)
# Let the queue know about this new serverPool
Hellanzb.queue.serverAdd(preWrappedNsf)
return connectionCount
0
Example 47
Project: pyGBot Source File: core.py
def run():
""" Run GBot. Called from the pyGBot bootstrap script. """
try:
conf = ConfigObj('pyGBot.ini')
except IOError, msg:
print "Cannot open config file: ", msg
sys.exit(1)
if conf.has_key('IRC') == False:
print "Config file does not contain IRC connection information."
sys.exit(1)
try:
channels = format.decodeIn(conf['IRC']['channel']).split()
host = conf['IRC']['host']
port = int(conf['IRC']['port'])
except ConfigObjError:
print "Required IRC connection info missing or invalid."
sys.exit(1)
localport = None
localaddr = None
if conf['IRC'].has_key('localport'):
localport = int(conf['IRC']['localport'])
if conf['IRC'].has_key('localaddr'):
localaddr = conf['IRC']['localaddr']
print "Initialising Factory..."
# create factory protocol and application
fact = GBotFactory(channels, 'UNUSED')
# SSL support
sslconnect = None
if conf['IRC'].has_key('ssl'):
if conf['IRC']['ssl'].lower() == "true":
from twisted.internet import ssl
# create SSL factory
cfact = ssl.ClientContextFactory()
sslconnect = True
print "Connecting..."
try:
# connect factory to this host and port, with SSL if enabled
if sslconnect:
if localaddr and localport:
GBotFactory.connector = reactor.connectSSL(host, port, fact, cfact, bindAddress=(localaddr, localport))
else:
GBotFactory.connector = reactor.connectSSL(host, port, fact, cfact)
else:
if localaddr and localport:
GBotFactory.connector = reactor.connectTCP(host, port, fact, bindAddress=(localaddr, localport))
else:
GBotFactory.connector = reactor.connectTCP(host, port, fact)
# run bot
reactor.run()
except:
print "Unexpected error:", sys.exc_info()[0]
raise
0
Example 48
def read(self):
"Connect to the feedback service and read all data."
log.msg('APNSService read (connecting)')
try:
server, port = ((FEEDBACK_SERVER_SANDBOX_HOSTNAME
if self.environment == 'sandbox'
else FEEDBACK_SERVER_HOSTNAME), FEEDBACK_SERVER_PORT)
factory = self.feedbackProtocolFactory()
context = self.getContextFactory()
reactor.connectSSL(server, port, factory, context)
factory.deferred.addErrback(log_errback('apns-feedback-read'))
timeout = reactor.callLater(self.timeout,
lambda: factory.deferred.called or factory.deferred.errback(
Exception('Feedbcak fetch timed out after %i seconds' % self.timeout)))
def cancel_timeout(r):
try: timeout.cancel()
except: pass
return r
factory.deferred.addBoth(cancel_timeout)
except Exception, e:
log.err('APNService feedback error initializing: %s' % str(e))
raise
return factory.deferred
0
Example 49
Project: filesync-server Source File: test_ssl_proxy.py
@unittest.skip('Should fail with connectionDone')
@defer.inlineCallbacks
def test_ssl_handshake_backend_dead(self):
"""No ssl handshake failure if the backend is dead."""
# turn off the backend
yield self.service.stopService()
self.addCleanup(self.service.startService)
# patch connectionMade to get a reference to the client.
client_d = defer.Deferred()
orig_connectionMade = StorageClient.connectionMade
def connectionMade(s):
"""Intercecpt connectionMade."""
orig_connectionMade(s)
client_d.callback(s)
self.patch(StorageClient, 'connectionMade', connectionMade)
f = StorageClientFactory()
# connect to the servr
reactor.connectSSL(
"localhost", self.ssl_port, f, ssl.ClientContextFactory())
storage_client = yield client_d
# try to do anything and fail with ConnectionDone
try:
yield storage_client.set_caps(PREFERRED_CAP)
except txerror.ConnectionDone:
pass
except OpenSSL.SSL.Error as e:
self.fail("Got %s" % e)
else:
self.fail("Should get a ConnectionDone.")
0
Example 50
def _download_file(url, filename, callback=None, headers=None, force_filename=False, allow_compression=True):
"""
Downloads a file from a specific URL and returns a Deferred. A callback
function can be specified to be called as parts are received.
Args:
url (str): The url to download from
filename (str): The filename to save the file as
callback (func): A function to be called when a part of data is received,
it's signature should be: func(data, current_length, total_length)
headers (dict): Any optional headers to send
force_filename (bool): force us to use the filename specified rather than
one the server may suggest
allow_compression (bool): Allows gzip & deflate decoding
Returns:
Deferred: the filename of the downloaded file
Raises:
t.w.e.PageRedirect
t.w.e.Error: for all other HTTP response errors
"""
url = str(url)
filename = str(filename)
if headers:
for key, value in headers.items():
headers[str(key)] = str(value)
if allow_compression:
if not headers:
headers = {}
headers['accept-encoding'] = 'deflate, gzip, x-gzip'
# In Twisted 13.1.0 _parse() function replaced by _URI class.
# In Twisted 15.0.0 _URI class renamed to URI.
if hasattr(client, '_parse'):
scheme, host, port, dummy_path = client._parse(url)
else:
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
factory = HTTPDownloader(url, filename, callback, headers, force_filename, allow_compression)
if scheme == 'https':
from twisted.internet import ssl
# ClientTLSOptions in Twisted >= 14, see ticket #2765 for details on this addition.
try:
from twisted.internet._sslverify import ClientTLSOptions
except ImportError:
ctx_factory = ssl.ClientContextFactory()
else:
class TLSSNIContextFactory(ssl.ClientContextFactory): # pylint: disable=no-init
"""
A custom context factory to add a server name for TLS connections.
"""
def getContext(self): # NOQA: N802
ctx = ssl.ClientContextFactory.getContext(self)
ClientTLSOptions(host, ctx)
return ctx
ctx_factory = TLSSNIContextFactory()
reactor.connectSSL(host, port, factory, ctx_factory)
else:
reactor.connectTCP(host, port, factory)
return factory.deferred