Skip to content

Commit f77d713

Browse files
authored
Merge pull request #5 from brentru/add-parse-headers
Add Parse_Headers Method Back
2 parents 37d277c + 6b18149 commit f77d713

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

adafruit_requests.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,26 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo
250250
resp.reason = reason
251251
return resp
252252

253-
254-
# pylint: enable=too-many-branches, too-many-statements, unused-argument
255-
# pylint: enable=too-many-arguments, too-many-locals
256-
253+
def parse_headers(sock):
254+
"""
255+
Parses the header portion of an HTTP request/response from the socket.
256+
Expects first line of HTTP request/response to have been read already
257+
return: header dictionary
258+
rtype: Dict
259+
"""
260+
headers = {}
261+
while True:
262+
line = sock.readline()
263+
if not line or line == b"\r\n":
264+
break
265+
266+
#print("**line: ", line)
267+
title, content = line.split(b': ', 1)
268+
if title and content:
269+
title = str(title.lower(), 'utf-8')
270+
content = str(content, 'utf-8')
271+
headers[title] = content
272+
return headers
257273

258274
def head(url, **kw):
259275
"""Send HTTP HEAD request"""

0 commit comments

Comments
 (0)