27
27
import six .moves
28
28
29
29
from requests .auth import HTTPBasicAuth
30
+ from urlparse import urlparse
30
31
31
32
from plotly import exceptions , tools , utils , version , files
32
33
from plotly .plotly import chunked_requests
@@ -439,6 +440,9 @@ class Stream:
439
440
440
441
"""
441
442
443
+ HTTP_PORT = 80
444
+ HTTPS_PORT = 443
445
+
442
446
@utils .template_doc (** tools .get_config_file ())
443
447
def __init__ (self , stream_id ):
444
448
"""
@@ -454,6 +458,29 @@ def __init__(self, stream_id):
454
458
self .connected = False
455
459
self ._stream = None
456
460
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
+
457
484
def heartbeat (self , reconnect_on = (200 , '' , 408 )):
458
485
"""
459
486
Keep stream alive. Streams will close after ~1 min of inactivity.
@@ -481,10 +508,8 @@ def open(self):
481
508
https://plot.ly/python/streaming/
482
509
483
510
"""
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 )
488
513
489
514
def write (self , trace , layout = None , validate = True ,
490
515
reconnect_on = (200 , '' , 408 )):
0 commit comments