Client update
This commit is contained in:
56
client.py
56
client.py
@@ -1,13 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import logging
|
||||
import sys
|
||||
import socket
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import os
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from client.ResponseHandler import ResponseHandler
|
||||
from client import ResponseHandler
|
||||
from client.httpclient import HTTPClient
|
||||
|
||||
FORMAT = 'utf-8'
|
||||
BUFSIZE = 4096
|
||||
@@ -125,22 +126,7 @@ def get_chunk(buffer: bytes):
|
||||
return buffer[:split_start], buffer[split_end:]
|
||||
|
||||
|
||||
def get_html_filename(headers):
|
||||
if "CONTENT-LOCATION" not in headers:
|
||||
return "index.html"
|
||||
|
||||
filename = headers["CONTENT-LOCATION"]
|
||||
result = os.path.basename(filename).strip()
|
||||
|
||||
if len(result.strip()) == 0:
|
||||
return 'index.html'
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def response_parser(client: socket.socket):
|
||||
client.settimeout(3.0)
|
||||
|
||||
try:
|
||||
buffer = client.recv(BUFSIZE)
|
||||
except TimeoutError as err:
|
||||
@@ -165,7 +151,7 @@ def response_parser(client: socket.socket):
|
||||
if payload_size == 0:
|
||||
return
|
||||
|
||||
filename = get_html_filename(headers)
|
||||
filename = util.get_html_filename(headers)
|
||||
|
||||
f = open(filename, "wb")
|
||||
f.write(buffer)
|
||||
@@ -199,6 +185,20 @@ def http_parser(client: socket.socket):
|
||||
logging.debug("chunk: %r", chunk)
|
||||
|
||||
|
||||
def parse_uri(uri: str):
|
||||
parsed = urlparse(uri)
|
||||
|
||||
# If there is no netloc, the url is invalid, so prepend `//` and try again
|
||||
if parsed.netloc == "":
|
||||
parsed = urlparse("//" + uri)
|
||||
|
||||
host = parsed.netloc
|
||||
path = parsed.path
|
||||
if len(path) == 0 or path[0] != '/':
|
||||
path = "/" + path
|
||||
return host, path
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='HTTP Client')
|
||||
parser.add_argument("--verbose", "-v", action='count', default=0, help="Increase verbosity level of logging")
|
||||
@@ -211,13 +211,19 @@ def main():
|
||||
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))
|
||||
(host, path) = parse_uri(arguments.URI)
|
||||
client = HTTPClient(host)
|
||||
client.connect((host, arguments.port))
|
||||
|
||||
message = "GET /Protocols/HTTP/Performance/microscape/ HTTP/1.1\r\nHost: www.w3.org:80\r\n\r\n".encode(FORMAT)
|
||||
message = "GET {path} HTTP/1.1\r\n".format(path=path)
|
||||
message += "Accept: */*\r\nAccept-Encoding: identity\r\n"
|
||||
message += "Host: {host}\r\n\r\n".format(host=host)
|
||||
|
||||
message = message.encode(FORMAT)
|
||||
logging.debug("Sending HTTP message: %r", message)
|
||||
client.sendall(message)
|
||||
|
||||
response_parser(client)
|
||||
ResponseHandler.handle(client, arguments.URI)
|
||||
# response_parser(client)
|
||||
# http_parser(client)
|
||||
# tmp = b''
|
||||
# keep = False
|
||||
|
Reference in New Issue
Block a user