Skip to content

Commit ce54830

Browse files
committed
add SSL support for streaming
1 parent 0a57c11 commit ce54830

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

plotly/plotly/plotly.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import six.moves
2828

2929
from requests.auth import HTTPBasicAuth
30+
from urlparse import urlparse
3031

3132
from plotly import exceptions, tools, utils, version, files
3233
from plotly.plotly import chunked_requests
@@ -439,6 +440,9 @@ class Stream:
439440
440441
"""
441442

443+
HTTP_PORT = 80
444+
HTTPS_PORT = 443
445+
442446
@utils.template_doc(**tools.get_config_file())
443447
def __init__(self, stream_id):
444448
"""
@@ -454,6 +458,29 @@ def __init__(self, stream_id):
454458
self.connected = False
455459
self._stream = None
456460

461+
def get_streaming_specs(self):
462+
"""
463+
Returns the streaming server, port, ssl_enabled flag, and headers.
464+
465+
"""
466+
streaming_url = get_config()['plotly_streaming_domain']
467+
ssl_enabled = 'https' in streaming_url
468+
port = self.HTTPS_PORT if ssl_enabled else self.HTTP_PORT
469+
470+
# If no scheme (https/https) is included in the streaming_url, the
471+
# host will be None. Use streaming_url in this case.
472+
host = urlparse(streaming_url).hostname or streaming_url
473+
474+
headers = {'Host': host, 'plotly-streamtoken': self.stream_id}
475+
streaming_specs = {
476+
'server': host,
477+
'port': port,
478+
'ssl_enabled': ssl_enabled,
479+
'headers': headers
480+
}
481+
482+
return streaming_specs
483+
457484
def heartbeat(self, reconnect_on=(200, '', 408)):
458485
"""
459486
Keep stream alive. Streams will close after ~1 min of inactivity.
@@ -481,10 +508,8 @@ def open(self):
481508
https://plot.ly/python/streaming/
482509
483510
"""
484-
streaming_url = get_config()['plotly_streaming_domain']
485-
self._stream = chunked_requests.Stream(
486-
streaming_url, 80, {'Host': streaming_url,
487-
'plotly-streamtoken': self.stream_id})
511+
streaming_specs = self.get_streaming_specs()
512+
self._stream = chunked_requests.Stream(**streaming_specs)
488513

489514
def write(self, trace, layout=None, validate=True,
490515
reconnect_on=(200, '', 408)):

0 commit comments

Comments
 (0)