Created
March 28, 2019 18:14
-
-
Save tirinox/ad30f1b401a244275ffbfd88f6f85afd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import select | |
from urllib.parse import urlsplit | |
def brutal_download(url, save_to): | |
url_components = urlsplit(url) | |
host = url_components.netloc | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((host, 80)) | |
s.sendall(bytes(f'GET {url_components.path} HTTP/1.1\r\nHOST: {host}\r\n\r\n', 'utf-8')) | |
reply = b'' | |
while select.select([s], [], [], 3)[0]: | |
data = s.recv(2048) | |
if not data: | |
break | |
reply += data | |
headers = reply.split(b'\r\n\r\n')[0] | |
full_data = reply[len(headers)+4:] | |
# save image | |
with open(save_to, 'wb') as f: | |
f.write(full_data) | |
brutal_download('http://image-net.org/logo.png', 'logo.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment