summaryrefslogtreecommitdiff
path: root/curl.py
blob: 1a73c194c0acf12834bfeee64b190a9aad97555f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3

import sys
import urllib.parse

import tulip
from tulip import http_client


def main():
    url = sys.argv[1]
    scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
    if not path:
        path = '/'
    if query:
        path = '?'.join([path, query])
    print(netloc, path, scheme)
    p = http_client.HttpClientProtocol(netloc, path=path,
                                       ssl=(scheme=='https'))
    f = p.connect()
    sts, headers, stream = p.event_loop.run_until_complete(tulip.Task(f))
    print(sts)
    for k, v in headers.items():
        print('{}: {}'.format(k, v))
    print()
    data = p.event_loop.run_until_complete(tulip.Task(stream.read(1000000)))
    print(data.decode('utf-8', 'replace'))


if __name__ == '__main__':
    main()