@@ -33,12 +33,12 @@ def bytes_next(self):
|
|||||||
return response.data
|
return response.data
|
||||||
|
|
||||||
|
|
||||||
def str_next(self):
|
def str_next(instance):
|
||||||
if self.status == 'eof':
|
if instance.status == 'eof':
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
response = self.response
|
response = instance.response
|
||||||
crlf = b'\r\n'
|
crlf = b'\r\n'
|
||||||
if self.position == 'status':
|
if instance.position == 'status':
|
||||||
status = ' '.join([
|
status = ' '.join([
|
||||||
response.version,
|
response.version,
|
||||||
str(response.code),
|
str(response.code),
|
||||||
@@ -46,19 +46,19 @@ def str_next(self):
|
|||||||
])
|
])
|
||||||
raw = status.encode()
|
raw = status.encode()
|
||||||
raw += crlf
|
raw += crlf
|
||||||
self.position = 'headers'
|
instance.position = 'headers'
|
||||||
return raw
|
return raw
|
||||||
if self.position == 'headers':
|
if instance.position == 'headers':
|
||||||
self.position = 'body'
|
instance.position = 'body'
|
||||||
raw = b''
|
raw = b''
|
||||||
for key in response.headers:
|
for key in response.headers:
|
||||||
value = response.headers[key]
|
value = response.headers[key]
|
||||||
raw += key.encode() + b' ' + value.encode() + crlf
|
raw += key.encode() + b' ' + value.encode() + crlf
|
||||||
raw += crlf
|
raw += crlf
|
||||||
return raw
|
return raw
|
||||||
if self.position == 'body':
|
if instance.position == 'body':
|
||||||
raw = response.data.encode()
|
raw = response.data.encode()
|
||||||
self.status = 'eof'
|
instance.status = 'eof'
|
||||||
return raw
|
return raw
|
||||||
|
|
||||||
|
|
||||||
@@ -105,17 +105,18 @@ class ResponseStream:
|
|||||||
self.status = 'running'
|
self.status = 'running'
|
||||||
self.position = 'status'
|
self.position = 'status'
|
||||||
response = self.response
|
response = self.response
|
||||||
print(type(response))
|
|
||||||
print(type(response.data))
|
|
||||||
if isinstance(response.data, str):
|
if isinstance(response.data, str):
|
||||||
self.__next__ = str_next
|
self.handler = str_next
|
||||||
elif isinstance(response.data, BufferedReader):
|
elif isinstance(response.data, BufferedReader):
|
||||||
self.__next__ = stream_next
|
self.handler = stream_next
|
||||||
elif isinstance(response.data, bytes):
|
elif isinstance(response.data, bytes):
|
||||||
self.__next__ = bytes_next
|
self.handler = bytes_next
|
||||||
else:
|
|
||||||
raise ValueError(type(response.data))
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
pass
|
return self.handler(self)
|
||||||
|
|
||||||
|
def handler(self):
|
||||||
|
response = self.response
|
||||||
|
print('data type:', type(response.data))
|
||||||
|
raise ValueError('data type error')
|
||||||
|
|||||||
Reference in New Issue
Block a user