Skip to content

Instantly share code, notes, and snippets.

@chimeno
Last active May 27, 2017 15:57
Show Gist options
  • Save chimeno/2e2e78dce1d07e5f5f49424299cec27f to your computer and use it in GitHub Desktop.
Save chimeno/2e2e78dce1d07e5f5f49424299cec27f to your computer and use it in GitHub Desktop.

Revisions

  1. @dchimeno dchimeno revised this gist May 27, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions script.py
    Original file line number Diff line number Diff line change
    @@ -24,12 +24,12 @@ def count_things(url):
    if line and line[0].isdigit(): # Start the data content
    tip_amount = float(line.split(',')[15]) # tip_amount column
    num_lines +=1

    total_tips += tip_amount
    writer.close()
    print(total_tips/num_lines)

    url = sys.argv[1]
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(count_things(url))
    loop.run_until_complete(task)
    loop.close()
    loop.close()
  2. @dchimeno dchimeno created this gist May 27, 2017.
    35 changes: 35 additions & 0 deletions script.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import asyncio
    import urllib.parse
    import sys

    @asyncio.coroutine
    def count_things(url):
    url = urllib.parse.urlsplit(url)
    if url.scheme == 'https':
    connect = asyncio.open_connection(url.hostname, 443, ssl=True)
    else:
    connect = asyncio.open_connection(url.hostname, 80)
    reader, writer = yield from connect
    query = ('GET {path} HTTP/1.1\r\n'
    'Host: {hostname}\r\n'
    '\r\n').format(path=url.path or '/', hostname=url.hostname)
    writer.write(query.encode('utf-8'))
    num_lines = 0
    total_tips = 0
    while True:
    line = yield from reader.readline()
    if not line:
    break
    line = line.decode('utf-8').rstrip()
    if line and line[0].isdigit(): # Start the data content
    tip_amount = float(line.split(',')[15]) # tip_amount column
    num_lines +=1

    writer.close()
    print(total_tips/num_lines)

    url = sys.argv[1]
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(count_things(url))
    loop.run_until_complete(task)
    loop.close()