Skip to content

Commit 1881cd1

Browse files
committed
Add utility function to parse raw HTTP strings.
1 parent 630005a commit 1881cd1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

plotly/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import re
1111
import sys
1212
import threading
13+
from StringIO import StringIO
1314

1415
import pytz
16+
import six.moves
1517

1618
try:
1719
import numpy
@@ -347,6 +349,27 @@ def is_source_key(key):
347349
return False
348350

349351

352+
# TODO: unclear if there's an async version to hook to our StreamReader.
353+
class HttpResponseParser(object):
354+
"""To parse a raw http response, we need to fake a socket."""
355+
356+
class FakeSocket(object):
357+
def __init__(self, response_str):
358+
self._file = StringIO(response_str)
359+
360+
def makefile(self, *args, **kwargs):
361+
return self._file
362+
363+
def __init__(self, response_string):
364+
self.response_string = response_string
365+
366+
def get_response(self):
367+
sock = self.FakeSocket(self.response_string)
368+
response = six.moves.http_client.HTTPResponse(sock)
369+
response.begin()
370+
return response
371+
372+
350373
class DisconnectThread(threading.Thread):
351374
"""Provides a disconnect api to communicate with threads."""
352375

0 commit comments

Comments
 (0)