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

Skip to content

Commit b15aa1c

Browse files
committed
Merge pull request plotly#495 from plotly/stream_ssl_verification
add ssl_verification_enabled to streaming_specs + tests
2 parents b989572 + 3e72710 commit b15aa1c

File tree

6 files changed

+45
-11
lines changed

6 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
66

7+
## [1.12.0] - 2016-06-06
8+
### Added
9+
- Added ability to enable/disable SSL certificate verification for streaming. Disabling SSL certification verification requires Python v2.7.9 / v3.4.3 (or above). This feature can be toggled via the `plotly_ssl_verification` configuration setting.
10+
711
## [1.11.0] - 2016-05-27
812
### Updated
913
- Changed the default option for `create_distplot` in the figure factory from `probability` to `probability density` and also added the `histnorm` parameter to allow the user to choose between the two options.

plotly/plotly/chunked_requests/chunked_request.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import time
22
import six
33
import os
4+
import ssl
5+
46
from six.moves import http_client
57
from six.moves.urllib.parse import urlparse
6-
from ssl import SSLError
78

89

910
class Stream:
10-
def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False):
11+
def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False,
12+
ssl_verification_enabled=True):
1113
''' Initialize a stream object and an HTTP or HTTPS connection
1214
with chunked Transfer-Encoding to server:port with optional headers.
1315
'''
@@ -20,6 +22,7 @@ def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False):
2022
self._headers = headers
2123
self._url = url
2224
self._ssl_enabled = ssl_enabled
25+
self._ssl_verification_enabled = ssl_verification_enabled
2326
self._connect()
2427

2528
def write(self, data, reconnect_on=('', 200, )):
@@ -99,6 +102,19 @@ def _get_proxy_config(self):
99102

100103
return proxy_server, proxy_port
101104

105+
def _get_ssl_context(self):
106+
"""
107+
Return an unverified context if ssl verification is disabled.
108+
109+
"""
110+
111+
context = None
112+
113+
if not self._ssl_verification_enabled:
114+
context = ssl._create_unverified_context()
115+
116+
return context
117+
102118
def _connect(self):
103119
''' Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding
104120
to server:port with optional headers.
@@ -111,8 +127,9 @@ def _connect(self):
111127

112128
if (proxy_server and proxy_port):
113129
if ssl_enabled:
130+
context = self._get_ssl_context()
114131
self._conn = http_client.HTTPSConnection(
115-
proxy_server, proxy_port
132+
proxy_server, proxy_port, context=context
116133
)
117134
else:
118135
self._conn = http_client.HTTPConnection(
@@ -121,7 +138,10 @@ def _connect(self):
121138
self._conn.set_tunnel(server, port)
122139
else:
123140
if ssl_enabled:
124-
self._conn = http_client.HTTPSConnection(server, port)
141+
context = self._get_ssl_context()
142+
self._conn = http_client.HTTPSConnection(
143+
server, port, context=context
144+
)
125145
else:
126146
self._conn = http_client.HTTPConnection(server, port)
127147

@@ -254,7 +274,7 @@ def _isconnected(self):
254274
# let's just assume that we're still connected and
255275
# hopefully recieve some data on the next try.
256276
return True
257-
elif isinstance(e, SSLError):
277+
elif isinstance(e, ssl.SSLError):
258278
if e.errno == 2:
259279
# errno 2 occurs when trying to read or write data, but more
260280
# data needs to be received on the underlying TCP transport

plotly/plotly/plotly.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ def get_streaming_specs(self):
463463
464464
"""
465465
streaming_url = get_config()['plotly_streaming_domain']
466+
ssl_verification_enabled = get_config()['plotly_ssl_verification']
466467
ssl_enabled = 'https' in streaming_url
467468
port = self.HTTPS_PORT if ssl_enabled else self.HTTP_PORT
468469

@@ -476,6 +477,7 @@ def get_streaming_specs(self):
476477
'server': host,
477478
'port': port,
478479
'ssl_enabled': ssl_enabled,
480+
'ssl_verification_enabled': ssl_verification_enabled,
479481
'headers': headers
480482
}
481483

plotly/tests/test_core/test_stream/test_stream.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
tk = 'vaia8trjjb'
1919
config = {'plotly_domain': 'https://plot.ly',
2020
'plotly_streaming_domain': 'stream.plot.ly',
21-
'plotly_api_domain': 'https://api.plot.ly'}
21+
'plotly_api_domain': 'https://api.plot.ly',
22+
'plotly_ssl_verification': False}
2223

2324

2425
class TestStreaming(TestCase):
@@ -127,6 +128,7 @@ def test_stream_no_scheme(self):
127128
'server': 'stream.plot.ly',
128129
'port': 80,
129130
'ssl_enabled': False,
131+
'ssl_verification_enabled': False,
130132
'headers': {
131133
'Host': 'stream.plot.ly',
132134
'plotly-streamtoken': tk
@@ -147,6 +149,7 @@ def test_stream_http(self):
147149
'server': 'stream.plot.ly',
148150
'port': 80,
149151
'ssl_enabled': False,
152+
'ssl_verification_enabled': False,
150153
'headers': {
151154
'Host': 'stream.plot.ly',
152155
'plotly-streamtoken': tk
@@ -158,15 +161,20 @@ def test_stream_http(self):
158161
def test_stream_https(self):
159162

160163
# If the https scheme is used in the plotly_streaming_domain, port 443
161-
# should be used for streaming and ssl_enabled should be True
164+
# should be used for streaming, ssl_enabled should be True,
165+
# and ssl_verification_enabled should equal plotly_ssl_verification
162166

163-
py.sign_in(un, ak,
164-
**{'plotly_streaming_domain': 'https://stream.plot.ly'})
167+
ssl_stream_config = {
168+
'plotly_streaming_domain': 'https://stream.plot.ly',
169+
'plotly_ssl_verification': True
170+
}
171+
py.sign_in(un, ak, **ssl_stream_config)
165172
my_stream = py.Stream(tk)
166173
expected_streaming_specs = {
167174
'server': 'stream.plot.ly',
168175
'port': 443,
169176
'ssl_enabled': True,
177+
'ssl_verification_enabled': True,
170178
'headers': {
171179
'Host': 'stream.plot.ly',
172180
'plotly-streamtoken': tk

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.11.0'
1+
__version__ = '1.12.0'

0 commit comments

Comments
 (0)