Skip to content

Commit a59e276

Browse files
authored
Adding --port option to pubsub.py client (take 2) (#195)
- Added `--port` option to be able to set the port when connecting with MQTT/TLS - Builder now uses default values if any arg is `None`. Previously, omitting an arg was the only way to get defaults. - Builder properly sets ALPN string if user passes in port=443
1 parent 0e4e91f commit a59e276

19 files changed

+1513
-1404
lines changed

awsiot/mqtt_connection_builder.py

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
**client_id** (`str`): ID to place in CONNECT packet. Must be unique across all devices/clients.
1212
If an ID is already in use, the other client will be disconnected.
1313
14-
Optional Keyword Arguments:
14+
Optional Keyword Arguments (omit, or set `None` to get default value):
1515
1616
**on_connection_interrupted** (`Callable`): Callback invoked whenever the MQTT connection is lost.
1717
The MQTT client will automatically attempt to reconnect.
@@ -106,6 +106,20 @@ def _check_required_kwargs(**kwargs):
106106
raise TypeError("Builder needs keyword-only argument '{}'".format(required))
107107

108108

109+
def _get(kwargs, name, default=None):
110+
"""
111+
Returns kwargs['name'] if it exists and is not None.
112+
Otherwise returns default.
113+
114+
This function exists so users can pass some_arg=None to get its default
115+
value, instead of literally passing None.
116+
"""
117+
val = kwargs.get(name)
118+
if val is None:
119+
val = default
120+
return val
121+
122+
109123
_metrics_str = None
110124

111125

@@ -132,64 +146,64 @@ def _builder(
132146
websocket_proxy_options=None,
133147
**kwargs):
134148

135-
ca_bytes = kwargs.get('ca_bytes')
136-
ca_filepath = kwargs.get('ca_filepath')
137-
ca_dirpath = kwargs.get('ca_dirpath')
149+
ca_bytes = _get(kwargs, 'ca_bytes')
150+
ca_filepath = _get(kwargs, 'ca_filepath')
151+
ca_dirpath = _get(kwargs, 'ca_dirpath')
138152
if ca_bytes:
139153
tls_ctx_options.override_default_trust_store(ca_bytes)
140154
elif ca_filepath or ca_dirpath:
141155
tls_ctx_options.override_default_trust_store_from_path(ca_dirpath, ca_filepath)
142156

143-
if use_websockets:
144-
port = 443
145-
if awscrt.io.is_alpn_available():
146-
tls_ctx_options.alpn_list = ['http/1.1']
147-
else:
148-
port = 8883
149-
if awscrt.io.is_alpn_available():
157+
port = _get(kwargs, 'port')
158+
if port is None:
159+
# prefer 443, even for direct MQTT connections, since it's less likely to be blocked by firewalls
160+
if use_websockets or awscrt.io.is_alpn_available():
150161
port = 443
151-
tls_ctx_options.alpn_list = ['x-amzn-mqtt-ca']
162+
else:
163+
port = 8883
152164

153-
port = kwargs.get('port', port)
165+
if port == 443 and awscrt.io.is_alpn_available():
166+
tls_ctx_options.alpn_list = ['http/1.1'] if use_websockets else ['x-amzn-mqtt-ca']
154167

155168
socket_options = awscrt.io.SocketOptions()
156-
socket_options.connect_timeout_ms = kwargs.get('tcp_connect_timeout_ms', 5000)
169+
socket_options.connect_timeout_ms = _get(kwargs, 'tcp_connect_timeout_ms', 5000)
157170
# These have been inconsistent between keepalive/keep_alive. Resolve both for now to ease transition.
158-
socket_options.keep_alive = kwargs.get('tcp_keep_alive', kwargs.get('tcp_keepalive', False))
159-
socket_options.keep_alive_timeout_secs = kwargs.get(
160-
'tcp_keep_alive_timeout_secs', kwargs.get(
161-
'tcp_keepalive_timeout_secs', 0))
162-
socket_options.keep_alive_interval_secs = kwargs.get(
163-
'tcp_keep_alive_interval_secs', kwargs.get(
164-
'tcp_keepalive_interval_secs', 0))
165-
socket_options.keep_alive_max_probes = kwargs.get(
166-
'tcp_keep_alive_max_probes', kwargs.get(
167-
'tcp_keepalive_max_probes', 0))
168-
169-
username = kwargs.get('username', '')
170-
if kwargs.get('enable_metrics_collection', True):
171+
socket_options.keep_alive = \
172+
_get(kwargs, 'tcp_keep_alive', _get(kwargs, 'tcp_keepalive', False))
173+
174+
socket_options.keep_alive_timeout_secs = \
175+
_get(kwargs, 'tcp_keep_alive_timeout_secs', _get(kwargs, 'tcp_keepalive_timeout_secs', 0))
176+
177+
socket_options.keep_alive_interval_secs = \
178+
_get(kwargs, 'tcp_keep_alive_interval_secs', _get(kwargs, 'tcp_keepalive_interval_secs', 0))
179+
180+
socket_options.keep_alive_max_probes = \
181+
_get(kwargs, 'tcp_keep_alive_max_probes', _get(kwargs, 'tcp_keepalive_max_probes', 0))
182+
183+
username = _get(kwargs, 'username', '')
184+
if _get(kwargs, 'enable_metrics_collection', True):
171185
username += _get_metrics_str()
172186

173-
client_bootstrap = kwargs.get('client_bootstrap')
187+
client_bootstrap = _get(kwargs, 'client_bootstrap')
174188
tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options)
175189
mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx)
176190

177191
return awscrt.mqtt.Connection(
178192
client=mqtt_client,
179-
on_connection_interrupted=kwargs.get('on_connection_interrupted'),
180-
on_connection_resumed=kwargs.get('on_connection_resumed'),
181-
client_id=kwargs.get('client_id'),
182-
host_name=kwargs.get('endpoint'),
193+
on_connection_interrupted=_get(kwargs, 'on_connection_interrupted'),
194+
on_connection_resumed=_get(kwargs, 'on_connection_resumed'),
195+
client_id=_get(kwargs, 'client_id'),
196+
host_name=_get(kwargs, 'endpoint'),
183197
port=port,
184-
clean_session=kwargs.get('clean_session', False),
185-
reconnect_min_timeout_secs=kwargs.get('reconnect_min_timeout_secs', 5),
186-
reconnect_max_timeout_secs=kwargs.get('reconnect_max_timeout_secs', 60),
187-
keep_alive_secs=kwargs.get('keep_alive_secs', 1200),
188-
ping_timeout_ms=kwargs.get('ping_timeout_ms', 3000),
189-
protocol_operation_timeout_ms=kwargs.get('protocol_operation_timeout_ms', 0),
190-
will=kwargs.get('will'),
198+
clean_session=_get(kwargs, 'clean_session', False),
199+
reconnect_min_timeout_secs=_get(kwargs, 'reconnect_min_timeout_secs', 5),
200+
reconnect_max_timeout_secs=_get(kwargs, 'reconnect_max_timeout_secs', 60),
201+
keep_alive_secs=_get(kwargs, 'keep_alive_secs', 1200),
202+
ping_timeout_ms=_get(kwargs, 'ping_timeout_ms', 3000),
203+
protocol_operation_timeout_ms=_get(kwargs, 'protocol_operation_timeout_ms', 0),
204+
will=_get(kwargs, 'will'),
191205
username=username,
192-
password=kwargs.get('password'),
206+
password=_get(kwargs, 'password'),
193207
socket_options=socket_options,
194208
use_websockets=use_websockets,
195209
websocket_handshake_transform=websocket_handshake_transform,

docs/_static/basic.css

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ ul.search li a {
130130
font-weight: bold;
131131
}
132132

133-
ul.search li div.context {
133+
ul.search li p.context {
134134
color: #888;
135135
margin: 2px 0 0 30px;
136136
text-align: left;
@@ -277,25 +277,25 @@ p.rubric {
277277
font-weight: bold;
278278
}
279279

280-
img.align-left, .figure.align-left, object.align-left {
280+
img.align-left, figure.align-left, .figure.align-left, object.align-left {
281281
clear: left;
282282
float: left;
283283
margin-right: 1em;
284284
}
285285

286-
img.align-right, .figure.align-right, object.align-right {
286+
img.align-right, figure.align-right, .figure.align-right, object.align-right {
287287
clear: right;
288288
float: right;
289289
margin-left: 1em;
290290
}
291291

292-
img.align-center, .figure.align-center, object.align-center {
292+
img.align-center, figure.align-center, .figure.align-center, object.align-center {
293293
display: block;
294294
margin-left: auto;
295295
margin-right: auto;
296296
}
297297

298-
img.align-default, .figure.align-default {
298+
img.align-default, figure.align-default, .figure.align-default {
299299
display: block;
300300
margin-left: auto;
301301
margin-right: auto;
@@ -319,7 +319,8 @@ img.align-default, .figure.align-default {
319319

320320
/* -- sidebars -------------------------------------------------------------- */
321321

322-
div.sidebar {
322+
div.sidebar,
323+
aside.sidebar {
323324
margin: 0 0 0.5em 1em;
324325
border: 1px solid #ddb;
325326
padding: 7px;
@@ -377,12 +378,14 @@ div.body p.centered {
377378
/* -- content of sidebars/topics/admonitions -------------------------------- */
378379

379380
div.sidebar > :last-child,
381+
aside.sidebar > :last-child,
380382
div.topic > :last-child,
381383
div.admonition > :last-child {
382384
margin-bottom: 0;
383385
}
384386

385387
div.sidebar::after,
388+
aside.sidebar::after,
386389
div.topic::after,
387390
div.admonition::after,
388391
blockquote::after {
@@ -455,20 +458,22 @@ td > :last-child {
455458

456459
/* -- figures --------------------------------------------------------------- */
457460

458-
div.figure {
461+
div.figure, figure {
459462
margin: 0.5em;
460463
padding: 0.5em;
461464
}
462465

463-
div.figure p.caption {
466+
div.figure p.caption, figcaption {
464467
padding: 0.3em;
465468
}
466469

467-
div.figure p.caption span.caption-number {
470+
div.figure p.caption span.caption-number,
471+
figcaption span.caption-number {
468472
font-style: italic;
469473
}
470474

471-
div.figure p.caption span.caption-text {
475+
div.figure p.caption span.caption-text,
476+
figcaption span.caption-text {
472477
}
473478

474479
/* -- field list styles ----------------------------------------------------- */
@@ -503,6 +508,63 @@ table.hlist td {
503508
vertical-align: top;
504509
}
505510

511+
/* -- object description styles --------------------------------------------- */
512+
513+
.sig {
514+
font-family: 'Consolas', 'Menlo', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace;
515+
}
516+
517+
.sig-name, code.descname {
518+
background-color: transparent;
519+
font-weight: bold;
520+
}
521+
522+
.sig-name {
523+
font-size: 1.1em;
524+
}
525+
526+
code.descname {
527+
font-size: 1.2em;
528+
}
529+
530+
.sig-prename, code.descclassname {
531+
background-color: transparent;
532+
}
533+
534+
.optional {
535+
font-size: 1.3em;
536+
}
537+
538+
.sig-paren {
539+
font-size: larger;
540+
}
541+
542+
.sig-param.n {
543+
font-style: italic;
544+
}
545+
546+
/* C++ specific styling */
547+
548+
.sig-inline.c-texpr,
549+
.sig-inline.cpp-texpr {
550+
font-family: unset;
551+
}
552+
553+
.sig.c .k, .sig.c .kt,
554+
.sig.cpp .k, .sig.cpp .kt {
555+
color: #0033B3;
556+
}
557+
558+
.sig.c .m,
559+
.sig.cpp .m {
560+
color: #1750EB;
561+
}
562+
563+
.sig.c .s, .sig.c .sc,
564+
.sig.cpp .s, .sig.cpp .sc {
565+
color: #067D17;
566+
}
567+
506568

507569
/* -- other body styles ----------------------------------------------------- */
508570

@@ -629,14 +691,6 @@ dl.glossary dt {
629691
font-size: 1.1em;
630692
}
631693

632-
.optional {
633-
font-size: 1.3em;
634-
}
635-
636-
.sig-paren {
637-
font-size: larger;
638-
}
639-
640694
.versionmodified {
641695
font-style: italic;
642696
}
@@ -766,7 +820,11 @@ div.code-block-caption code {
766820
table.highlighttable td.linenos,
767821
span.linenos,
768822
div.doctest > div.highlight span.gp { /* gp: Generic.Prompt */
769-
user-select: none;
823+
user-select: none;
824+
-webkit-user-select: text; /* Safari fallback only */
825+
-webkit-user-select: none; /* Chrome/Safari */
826+
-moz-user-select: none; /* Firefox */
827+
-ms-user-select: none; /* IE10+ */
770828
}
771829

772830
div.code-block-caption span.caption-number {
@@ -781,16 +839,6 @@ div.literal-block-wrapper {
781839
margin: 1em 0;
782840
}
783841

784-
code.descname {
785-
background-color: transparent;
786-
font-weight: bold;
787-
font-size: 1.2em;
788-
}
789-
790-
code.descclassname {
791-
background-color: transparent;
792-
}
793-
794842
code.xref, a code {
795843
background-color: transparent;
796844
font-weight: bold;

docs/_static/pygments.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
pre { line-height: 125%; }
2+
td.linenos .normal { color: #666666; background-color: transparent; padding-left: 5px; padding-right: 5px; }
3+
span.linenos { color: #666666; background-color: transparent; padding-left: 5px; padding-right: 5px; }
4+
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
5+
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
16
.highlight .hll { background-color: #ffffcc }
2-
.highlight { background: #f0f0f0; }
7+
.highlight { background: #f0f0f0; }
38
.highlight .c { color: #60a0b0; font-style: italic } /* Comment */
49
.highlight .err { border: 1px solid #FF0000 } /* Error */
510
.highlight .k { color: #007020; font-weight: bold } /* Keyword */

docs/_static/searchtools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ var Search = {
509509
var excerpt = ((start > 0) ? '...' : '') +
510510
$.trim(text.substr(start, 240)) +
511511
((start + 240 - text.length) ? '...' : '');
512-
var rv = $('<div class="context"></div>').text(excerpt);
512+
var rv = $('<p class="context"></p>').text(excerpt);
513513
$.each(hlwords, function() {
514514
rv = rv.highlightText(this, 'highlighted');
515515
});

0 commit comments

Comments
 (0)