aboutsummaryrefslogtreecommitdiff
path: root/gemget.py
blob: c7bde68dded579c4af45fe56fdaafd7dac8df493 (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
import socket
import ssl
import sys
import urllib.parse

context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE

url = sys.argv[1]
split_url = urllib.parse.urlsplit(url)

with socket.create_connection((split_url.netloc, 1965)) as sock:
    with context.wrap_socket(sock, server_hostname=split_url.netloc) as ssock:
        ssock.sendall(url.encode("ascii") + b"\r\n")
        recv = b""
        while True:
            chunk = ssock.recv()
            if not chunk:
                break
            recv += chunk

header, rest = recv.split(b"\r\n", 1)
header = header.decode("utf8")
assert header.startswith("2"), f"Response header {header} is not 2*"

sys.stdout.buffer.write(rest)