-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Https stream #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Https stream #461
Changes from 5 commits
ce54830
5834a12
a4616e3
01f1c46
f46ef5f
4a2c82e
bf10b6c
e60d30c
d4b60cd
ff7a57d
f1ba31b
af21115
c71d4ce
2d08352
d9989ef
f8c2f46
897565e
e873d67
f0e8a5e
66db06d
9f7d44c
3c7a623
74c2bb0
0990b1d
811b5d7
1eeec5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import six.moves | ||
|
||
from requests.auth import HTTPBasicAuth | ||
from urlparse import urlparse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. brap. this isn't Python3 compat. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
from plotly import exceptions, tools, utils, version, files | ||
from plotly.plotly import chunked_requests | ||
|
@@ -439,6 +440,9 @@ class Stream: | |
|
||
""" | ||
|
||
HTTP_PORT = 80 | ||
HTTPS_PORT = 443 | ||
|
||
@utils.template_doc(**tools.get_config_file()) | ||
def __init__(self, stream_id): | ||
""" | ||
|
@@ -454,6 +458,29 @@ def __init__(self, stream_id): | |
self.connected = False | ||
self._stream = None | ||
|
||
def get_streaming_specs(self): | ||
""" | ||
Returns the streaming server, port, ssl_enabled flag, and headers. | ||
|
||
""" | ||
streaming_url = get_config()['plotly_streaming_domain'] | ||
ssl_enabled = 'https' in streaming_url | ||
port = self.HTTPS_PORT if ssl_enabled else self.HTTP_PORT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. I think we should default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I say we roll this out as is and then change the default behaviour once we know streaming over |
||
|
||
# If no scheme (https/https) is included in the streaming_url, the | ||
# host will be None. Use streaming_url in this case. | ||
host = urlparse(streaming_url).hostname or streaming_url | ||
|
||
headers = {'Host': host, 'plotly-streamtoken': self.stream_id} | ||
streaming_specs = { | ||
'server': host, | ||
'port': port, | ||
'ssl_enabled': ssl_enabled, | ||
'headers': headers | ||
} | ||
|
||
return streaming_specs | ||
|
||
def heartbeat(self, reconnect_on=(200, '', 408)): | ||
""" | ||
Keep stream alive. Streams will close after ~1 min of inactivity. | ||
|
@@ -481,10 +508,8 @@ def open(self): | |
https://plot.ly/python/streaming/ | ||
|
||
""" | ||
streaming_url = get_config()['plotly_streaming_domain'] | ||
self._stream = chunked_requests.Stream( | ||
streaming_url, 80, {'Host': streaming_url, | ||
'plotly-streamtoken': self.stream_id}) | ||
streaming_specs = self.get_streaming_specs() | ||
self._stream = chunked_requests.Stream(**streaming_specs) | ||
|
||
def write(self, trace, layout=None, validate=True, | ||
reconnect_on=(200, '', 408)): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import time | ||
|
||
from nose.plugins.attrib import attr | ||
from nose.tools import raises | ||
from nose.tools import raises, eq_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😿 we should really switch this over. Mind if I make a PR into yrs to update this? We should be doing class-based testing using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we really should. Please do! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool. on it. |
||
|
||
import plotly.plotly as py | ||
from plotly.graph_objs import (Layout, Scatter, Stream) | ||
|
@@ -123,3 +123,63 @@ def test_stream_unstreamable(): | |
my_stream.open() | ||
my_stream.write(Scatter(x=1, y=10, name='nope')) | ||
my_stream.close() | ||
|
||
|
||
def test_stream_no_scheme(): | ||
|
||
# If no scheme is used in the plotly_streaming_domain, port 80 | ||
# should be used for streaming and ssl_enabled should be False | ||
|
||
py.sign_in(un, ak, **{'plotly_streaming_domain': 'stream.plot.ly'}) | ||
my_stream = py.Stream(tk) | ||
expected_streaming_specs = { | ||
'server': 'stream.plot.ly', | ||
'port': 80, | ||
'ssl_enabled': False, | ||
'headers': { | ||
'Host': 'stream.plot.ly', | ||
'plotly-streamtoken': tk | ||
} | ||
} | ||
actual_streaming_specs = my_stream.get_streaming_specs() | ||
eq_(expected_streaming_specs, actual_streaming_specs) | ||
|
||
|
||
def test_stream_http(): | ||
|
||
# If the http scheme is used in the plotly_streaming_domain, port 80 | ||
# should be used for streaming and ssl_enabled should be False | ||
|
||
py.sign_in(un, ak, **{'plotly_streaming_domain': 'http://stream.plot.ly'}) | ||
my_stream = py.Stream(tk) | ||
expected_streaming_specs = { | ||
'server': 'stream.plot.ly', | ||
'port': 80, | ||
'ssl_enabled': False, | ||
'headers': { | ||
'Host': 'stream.plot.ly', | ||
'plotly-streamtoken': tk | ||
} | ||
} | ||
actual_streaming_specs = my_stream.get_streaming_specs() | ||
eq_(expected_streaming_specs, actual_streaming_specs) | ||
|
||
|
||
def test_stream_https(): | ||
|
||
# If the https scheme is used in the plotly_streaming_domain, port 443 | ||
# should be used for streaming and ssl_enabled should be True | ||
|
||
py.sign_in(un, ak, **{'plotly_streaming_domain': 'https://stream.plot.ly'}) | ||
my_stream = py.Stream(tk) | ||
expected_streaming_specs = { | ||
'server': 'stream.plot.ly', | ||
'port': 443, | ||
'ssl_enabled': True, | ||
'headers': { | ||
'Host': 'stream.plot.ly', | ||
'plotly-streamtoken': tk | ||
} | ||
} | ||
actual_streaming_specs = my_stream.get_streaming_specs() | ||
eq_(expected_streaming_specs, actual_streaming_specs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐄 no newline at the end of the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, fixed my settings. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__version__ = '1.9.11' | ||
__version__ = '1.10.0' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgot the
0