Created
August 5, 2018 22:53
-
-
Save theelous3/f0c3250cd2016dd7845367d678456661 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 time | |
from gzip import compress | |
def main(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', 8000)) | |
s.listen(4) | |
while True: | |
conn, addr = s.accept() | |
handle(conn) | |
def handle(conn): | |
print('<-\n' + conn.recv(4096).decode('utf-8')) | |
headers = [ | |
'HTTP/1.1 200 OK', | |
'Content-Type: text/plain', | |
'transfer-encoding: chunked', | |
'content-encoding: gzip' | |
] | |
send(conn, ('\r\n'.join(headers) + '\r\n\r\n').encode()) | |
time.sleep(0.1) | |
for i in range(4): | |
chunk = compress(b'a' * 100) | |
if len(chunk) > 0: | |
send_chunk(conn, chunk) | |
time.sleep(0.1) | |
send_chunk(conn, b'') | |
conn.close() | |
def send(conn, data): | |
print('->\n', data) | |
conn.send(data) | |
def send_chunk(conn, chunk): | |
send(conn, hex(len(chunk))[2:].encode() + b'\r\n' + chunk + b'\r\n') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment