From a9c6c296f0021fa877a7c1944ac6ec7594122c10 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 18 Oct 2014 15:17:39 +0300 Subject: [PATCH] uaiohttpclient: Initial implementation of the client. Can do GET requests for URL, nothing more. --- uaiohttpclient/uaiohttpclient.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 uaiohttpclient/uaiohttpclient.py diff --git a/uaiohttpclient/uaiohttpclient.py b/uaiohttpclient/uaiohttpclient.py new file mode 100644 index 00000000..e502e07b --- /dev/null +++ b/uaiohttpclient/uaiohttpclient.py @@ -0,0 +1,43 @@ +import uasyncio as asyncio + + +class ClientResponse: + + def __init__(self, reader): + self.content = reader + + def read(self, sz=-1): + return (yield from self.content.read(sz)) + + def __repr__(self): + return "" % (self.status, self.headers) + + +def request_raw(method, url): + try: + proto, dummy, host, path = url.split("/", 3) + except ValueError: + proto, dummy, host = url.split("/", 2) + path = "" + reader, writer = yield from asyncio.open_connection(host, 80) + query = "%s /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (method, path, host) + yield from writer.awrite(query.encode('latin-1')) +# yield from writer.close() + return reader + + +def request(method, url): + reader = yield from request_raw(method, url) + resp = ClientResponse(reader) + headers = [] + sline = yield from reader.readline() + protover, st, msg = sline.split(None, 2) + resp.status = int(st) + while True: + line = yield from reader.readline() + if not line or line == b"\r\n": + break + headers.append(line) + + resp.headers = headers + return resp