in AWSIoTDeviceDefenderAgentSDK/agent.py [0:0]
def connect(self):
"""Connect to AWS IoT"""
if not self.certificate_path or not self.private_key_path:
print("Missing credentials for authentication.")
exit(2)
# Spin up resources
event_loop_group = io.EventLoopGroup(1)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
if self.use_websocket == True:
proxy_options = None
if (self.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=self.proxy_host, port=self.proxy_port)
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
self.iot_client = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=self.host,
client_bootstrap=client_bootstrap,
region=self.signing_region,
credentials_provider=credentials_provider,
websocket_proxy_options=proxy_options,
ca_filepath=self.root_ca_path,
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,
client_id=self.client_id,
clean_session=False,
keep_alive_secs=6)
else:
self.iot_client = mqtt_connection_builder.mtls_from_path(
endpoint=self.host,
cert_filepath=self.certificate_path,
pri_key_filepath=self.private_key_path,
client_bootstrap=client_bootstrap,
ca_filepath=self.root_ca_path,
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,
client_id=self.client_id,
clean_session=False,
keep_alive_secs=6)
print("Connecting to {} with client ID '{}'...".format(
self.host, self.client_id))
connect_future = self.iot_client.connect()
# Future.result() waits until a result is available
connect_future.result()
sleep(2)