Skip to content

Instantly share code, notes, and snippets.

@un-def
Created July 25, 2018 18:41
Show Gist options
  • Save un-def/af32b4725fc794cc64546aece5201aab to your computer and use it in GitHub Desktop.
Save un-def/af32b4725fc794cc64546aece5201aab to your computer and use it in GitHub Desktop.
dead simple asyncio http client
from http.client import HTTPResponse
import urllib.parse
import asyncio
import json
import sys
import io
class FakeSocket:
def __init__(self, response_bytes):
self._file = io.BytesIO(response_bytes)
def makefile(self, *args, **kwargs):
return self._file
async def fetch(url, *, method='GET'):
url = urllib.parse.urlsplit(url)
hostname = url.hostname
path = url.path or '/'
if url.query:
path = f'{path}?{url.query}'
if url.scheme == 'https':
port = url.port or 443
ssl = True
else:
port = url.port or 80
ssl = False
connect = asyncio.open_connection(hostname, port, ssl=ssl)
reader, writer = await connect
query = (
f'{method} {path} HTTP/1.0\r\n'
f'Host: {hostname}\r\n'
f'User-Agent: asyncioclient/0.0.0\r\n'
f'\r\n'
)
writer.write(query.encode('latin-1'))
response_bytes = await reader.read()
writer.close()
response = HTTPResponse(FakeSocket(response_bytes))
response.begin()
return response
async def main(url):
response = await fetch(url, method='GET')
print('STATUS:', response.status)
print('HEADERS:')
for header, value in response.getheaders():
print(f' {header}: {value}')
body = response.read()
try:
body = json.dumps(json.loads(body), ensure_ascii=False, indent=4)
except ValueError:
pass
print('BODY:', body)
if __name__ == '__main__':
try:
url = sys.argv[1]
except IndexError:
url = 'https://wtfismyip.com/json'
loop = asyncio.get_event_loop()
loop.run_until_complete(main(url))
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment