update
This commit is contained in:
81
client.py
81
client.py
@@ -2,6 +2,44 @@
|
||||
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')
|
||||
@@ -13,8 +51,47 @@ def main():
|
||||
arguments = parser.parse_args()
|
||||
|
||||
logging.basicConfig(level=logging.ERROR - (10 * arguments.verbose))
|
||||
logging.debug("Arguments: %r", arguments)
|
||||
raise Exception("dsfgsfsd")
|
||||
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:
|
||||
|
0
httplib/__init__.py
Normal file
0
httplib/__init__.py
Normal file
@@ -1 +0,0 @@
|
||||
sockets==1.0.0
|
||||
|
34
server.py
34
server.py
@@ -1 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import socket
|
||||
|
||||
# socket heeft een listening and accept method
|
||||
|
||||
SERVER = "127.0.0.1" #dynamisch fixen in project
|
||||
PORT = 5055
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
ADDR = (SERVER, PORT) # hier wordt de socket gebonden aan mijn IP adres, dit moet wel anders
|
||||
server.bind(ADDR) # in het project gebeuren
|
||||
|
||||
HEADER = 64 # maximum size messages
|
||||
FORMAT = 'utf-8' # recieving images through this format does not work
|
||||
DISCONNECT_MESSAGE = "DISCONNECT!" # special message for disconnecting client and server
|
||||
|
||||
# function for starting server
|
||||
def start():
|
||||
pass
|
||||
server.listen()
|
||||
while True: # infinite loop in which server accept incoming connections, we want to run it forever
|
||||
conn, addr = server.accept() # Server blocks untill a client connects
|
||||
print("new connection: ", addr[0], " connected.")
|
||||
connected = True
|
||||
while connected: # while client is connected, we want to recieve messages
|
||||
msg = conn.recv(HEADER).decode(FORMAT).rstrip() # Argument is maximum size of msg (in project look into details of accp), decode is for converting bytes to strings, rstrip is for stripping messages for special hidden characters
|
||||
print("message: ", msg)
|
||||
if msg == DISCONNECT_MESSAGE:
|
||||
connected = False
|
||||
print("close connection ", addr[0], " disconnected.")
|
||||
conn.close()
|
||||
|
||||
print("server is starting ... ")
|
||||
start()
|
Reference in New Issue
Block a user