Skip to content

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

Merged
merged 26 commits into from
May 17, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ce54830
add SSL support for streaming
BRONSOLO May 16, 2016
5834a12
add tests for streaming over https
BRONSOLO May 16, 2016
a4616e3
version update
BRONSOLO May 16, 2016
01f1c46
update changelog
BRONSOLO May 16, 2016
f46ef5f
version fix
BRONSOLO May 16, 2016
4a2c82e
fix version 1.1.0 ---> 1.10.0
BRONSOLO May 16, 2016
bf10b6c
fix missing newline
BRONSOLO May 16, 2016
e60d30c
use six for urllib for compat with Python 3
BRONSOLO May 16, 2016
d4b60cd
remove unused import
BRONSOLO May 16, 2016
ff7a57d
Run `make pull_chunked` and `make sync_chunked`.
theengineear May 16, 2016
f1ba31b
update python version for tests
BRONSOLO May 16, 2016
af21115
Merge branch 'https_stream' of https://github.com/plotly/plotly.py in…
BRONSOLO May 16, 2016
c71d4ce
update python to latest
BRONSOLO May 16, 2016
2d08352
use 2.7.10 (2.7.11 not available in circle yet)
BRONSOLO May 17, 2016
d9989ef
increase timeout to 1hr
BRONSOLO May 17, 2016
f8c2f46
fix indentation
BRONSOLO May 17, 2016
897565e
go back to using 2.8 (2.9 no longer required)
BRONSOLO May 17, 2016
e873d67
remove timeout
BRONSOLO May 17, 2016
f0e8a5e
fix circle template
BRONSOLO May 17, 2016
66db06d
Run `make pull_chunked` and `make sync_chunked`.
theengineear May 17, 2016
9f7d44c
Merge branch 'https_stream' of https://github.com/plotly/python-api i…
theengineear May 17, 2016
3c7a623
Use `TestCase` in `test_stream.py` tests.
theengineear May 16, 2016
74c2bb0
Merge pull request #462 from plotly/update-streaming-tests
theengineear May 17, 2016
0990b1d
space fix
BRONSOLO May 17, 2016
811b5d7
update version + fix changeling
BRONSOLO May 17, 2016
1eeec5a
Merge branch 'https_stream' of https://github.com/plotly/plotly.py in…
BRONSOLO May 17, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [1.1.0] - 2016-05-16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot the 0

### Added
SSL support for streaming. Requires the SSL module, which was backported in
Python v2.7.9, via chunked_requests submodule.

## [1.9.11] - 2016-05-02
### Added
- The FigureFactory can now create scatter plot matrices with `.create_scatterplotmatrix`. Check it out with:
Expand Down
33 changes: 29 additions & 4 deletions plotly/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import six.moves

from requests.auth import HTTPBasicAuth
from urlparse import urlparse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brap. this isn't Python3 compat.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -439,6 +440,9 @@ class Stream:

"""

HTTP_PORT = 80
HTTPS_PORT = 443

@utils.template_doc(**tools.get_config_file())
def __init__(self, stream_id):
"""
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I think we should default to 443 instead of 80 from now on. so like port = self.HTTP_PORT if 'http:' in streaming_url else self.HTTPS_PORT

Copy link
Member Author

Choose a reason for hiding this comment

The 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 https is performing as expected (beyond my local tests).


# 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.
Expand Down Expand Up @@ -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)):
Expand Down
62 changes: 61 additions & 1 deletion plotly/tests/test_core/test_stream/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time

from nose.plugins.attrib import attr
from nose.tools import raises
from nose.tools import raises, eq_
Copy link
Contributor

Choose a reason for hiding this comment

The 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 TestCase

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we really should. Please do!

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐄 no newline at the end of the file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, fixed my settings.

2 changes: 1 addition & 1 deletion plotly/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '1.9.11'
__version__ = '1.10.0'