Skip to content

Commit

Permalink
update chunked requests sub mods.
Browse files Browse the repository at this point in the history
  • Loading branch information
BRONSOLO committed Jun 6, 2016
1 parent 32e4edd commit 3e72710
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions plotly/plotly/chunked_requests/chunked_request.py
@@ -1,13 +1,15 @@
import time
import six
import os
import ssl

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='/', ssl_enabled=False):
def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False,
ssl_verification_enabled=True):
''' Initialize a stream object and an HTTP or HTTPS connection
with chunked Transfer-Encoding to server:port with optional headers.
'''
Expand All @@ -20,6 +22,7 @@ def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False):
self._headers = headers
self._url = url
self._ssl_enabled = ssl_enabled
self._ssl_verification_enabled = ssl_verification_enabled
self._connect()

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

return proxy_server, proxy_port

def _get_ssl_context(self):
"""
Return an unverified context if ssl verification is disabled.
"""

context = None

if not self._ssl_verification_enabled:
context = ssl._create_unverified_context()

return context

def _connect(self):
''' Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding
to server:port with optional headers.
Expand All @@ -111,8 +127,9 @@ def _connect(self):

if (proxy_server and proxy_port):
if ssl_enabled:
context = self._get_ssl_context()
self._conn = http_client.HTTPSConnection(
proxy_server, proxy_port
proxy_server, proxy_port, context=context
)
else:
self._conn = http_client.HTTPConnection(
Expand All @@ -121,7 +138,10 @@ def _connect(self):
self._conn.set_tunnel(server, port)
else:
if ssl_enabled:
self._conn = http_client.HTTPSConnection(server, port)
context = self._get_ssl_context()
self._conn = http_client.HTTPSConnection(
server, port, context=context
)
else:
self._conn = http_client.HTTPConnection(server, port)

Expand Down Expand Up @@ -254,7 +274,7 @@ 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):
elif isinstance(e, ssl.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
Expand Down
2 changes: 1 addition & 1 deletion submodules/chunked_requests
Submodule chunked_requests updated 3 files
+25 −5 chunked_requests/chunked_request.py
+2 −0 circle.yml
+20 −1 test/tests.py

0 comments on commit 3e72710

Please sign in to comment.