103 lines
2.6 KiB
Python
103 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import logging
|
|
import sys
|
|
import socket
|
|
|
|
FORMAT = 'utf-8'
|
|
BUFSIZE = 4096
|
|
|
|
|
|
def receive_bytes(client: socket.socket):
|
|
buffering = True
|
|
buffer = b''
|
|
while buffering:
|
|
received = client.recv(BUFSIZE)
|
|
received_size = len(received)
|
|
logging.debug("Received size: %s", received_size)
|
|
logging.debug("Received: %r", received)
|
|
|
|
if received_size < BUFSIZE:
|
|
buffering = False
|
|
|
|
buffer += received
|
|
buffer_split = buffer.split(b"\r\n\r\n")
|
|
buffer = buffer_split[-1]
|
|
|
|
for part in buffer_split[:-1]:
|
|
yield part + b"\r\n\r\n"
|
|
|
|
if buffer:
|
|
yield buffer
|
|
|
|
def http_parser(client: socket.socket):
|
|
headers = {}
|
|
start_line = ""
|
|
|
|
counter = 0
|
|
for received in receive_bytes(client):
|
|
if counter == 0:
|
|
|
|
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='HTTP Client')
|
|
parser.add_argument("--verbose", "-v", action='count', default=0, help="Increase verbosity level of logging")
|
|
parser.add_argument("--command", "-c", help="HEAD, GET, PUT or POST", default="GET")
|
|
parser.add_argument("--port", "-p", help="The port used to connect with the server", default=80)
|
|
parser.add_argument("URI", help="The URI to connect to")
|
|
|
|
arguments = parser.parse_args()
|
|
|
|
logging.basicConfig(level=logging.ERROR - (10 * arguments.verbose))
|
|
logging.debug("Arguments: %s", arguments)
|
|
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
client.connect((arguments.URI, arguments.port))
|
|
|
|
message = "GET /httpgallery/chunked/chunkedimage.aspx HTTP/1.1\r\nHost: www.httpwatch.com:80\r\n\r\n".encode(FORMAT)
|
|
client.sendall(message)
|
|
|
|
tmp = b''
|
|
keep = False
|
|
count = 0
|
|
for line in receive_lines(client):
|
|
|
|
if count > 0:
|
|
tmp += line.rstrip(b"\r\n")
|
|
if keep:
|
|
count += 1
|
|
|
|
if line == b'\r\n':
|
|
keep = True
|
|
|
|
logging.debug('end of part 1')
|
|
|
|
logging.debug("attempt 2")
|
|
while True:
|
|
logging.debug("attempt")
|
|
keep = False
|
|
for line in receive_lines(client):
|
|
if line == b"0\r\n":
|
|
break
|
|
if keep:
|
|
tmp += line.rstrip(b"\r\n")
|
|
keep = True
|
|
|
|
if b"0\r\n" == line:
|
|
break
|
|
logging.debug("content: %s", tmp)
|
|
# logging.debug("content: %r", tmp.replace(b"\r\n", b"").decode("utf-8"))
|
|
|
|
f = open("test.jpeg", "wb")
|
|
f.write(tmp)
|
|
|
|
|
|
try:
|
|
main()
|
|
except Exception as e:
|
|
print("[ABRT] Internal error: " + str(e), file=sys.stderr)
|
|
logging.debug("Internal error", exc_info=e)
|
|
sys.exit(70)
|