Thanks to visit codestin.com
Credit goes to github.com

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 all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [1.9.12] - 2016-05-16
### Added
SSL support for streaming.

## [1.9.11] - 2016-05-02
### Added
- The FigureFactory can now create scatter plot matrices with `.create_scatterplotmatrix`. Check it out with:
Expand Down
46 changes: 37 additions & 9 deletions plotly/plotly/chunked_requests/chunked_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import os
from six.moves import http_client
from six.moves.urllib.parse import urlparse
from ssl import SSLError


class Stream:
def __init__(self, server, port=80, headers={}, url='/'):
''' Initialize a stream object and an HTTP Connection
def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False):
''' Initialize a stream object and an HTTP or HTTPS connection
with chunked Transfer-Encoding to server:port with optional headers.
'''
self.maxtries = 5
Expand All @@ -17,6 +19,7 @@ def __init__(self, server, port=80, headers={}, url='/'):
self._port = port
self._headers = headers
self._url = url
self._ssl_enabled = ssl_enabled
self._connect()

def write(self, data, reconnect_on=('', 200, )):
Expand Down Expand Up @@ -73,17 +76,21 @@ def write(self, data, reconnect_on=('', 200, )):
def _get_proxy_config(self):
"""
Determine if self._url should be passed through a proxy. If so, return
the appropriate proxy_server and proxy_port
the appropriate proxy_server and proxy_port. Assumes https_proxy is used
when ssl_enabled=True.

"""

proxy_server = None
proxy_port = None
ssl_enabled = self._ssl_enabled

## only doing HTTPConnection, so only use http_proxy
proxy = os.environ.get("http_proxy")
if ssl_enabled:
proxy = os.environ.get("https_proxy")
else:
proxy = os.environ.get("http_proxy")
no_proxy = os.environ.get("no_proxy")
no_proxy_url = no_proxy and self._url in no_proxy
no_proxy_url = no_proxy and self._server in no_proxy

if proxy and not no_proxy_url:
p = urlparse(proxy)
Expand All @@ -93,19 +100,30 @@ def _get_proxy_config(self):
return proxy_server, proxy_port

def _connect(self):
''' Initialize an HTTP connection with chunked Transfer-Encoding
''' Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding
to server:port with optional headers.
'''
server = self._server
port = self._port
headers = self._headers
ssl_enabled = self._ssl_enabled
proxy_server, proxy_port = self._get_proxy_config()

if (proxy_server and proxy_port):
self._conn = http_client.HTTPConnection(proxy_server, proxy_port)
if ssl_enabled:
self._conn = http_client.HTTPSConnection(
proxy_server, proxy_port
)
else:
self._conn = http_client.HTTPConnection(
proxy_server, proxy_port
)
self._conn.set_tunnel(server, port)
else:
self._conn = http_client.HTTPConnection(server, port)
if ssl_enabled:
self._conn = http_client.HTTPSConnection(server, port)
else:
self._conn = http_client.HTTPConnection(server, port)

self._conn.putrequest('POST', self._url)
self._conn.putheader('Transfer-Encoding', 'chunked')
Expand Down Expand Up @@ -236,6 +254,16 @@ def _isconnected(self):
# let's just assume that we're still connected and
# hopefully recieve some data on the next try.
return True
elif isinstance(e, SSLError):
if e.errno == 2:
# errno 2 occurs when trying to read or write data, but more
# data needs to be received on the underlying TCP transport
# before the request can be fulfilled.
#
# Python 2.7.9+ and Python 3.3+ give this its own exception,
# SSLWantReadError
return True
raise e
else:
# Unknown scenario
raise e
Expand Down
33 changes: 29 additions & 4 deletions plotly/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,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 +457,30 @@ 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 = (six.moves.urllib.parse.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
Loading