From 3c540b321f1c2176c5d75e7dddc933e8620a78f2 Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 18 Nov 2021 15:03:34 +0300 Subject: [PATCH 1/7] Update server.py --- server.py | 61 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/server.py b/server.py index 9ed7429..7026a82 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,36 @@ import socket -sock = socket.socket() - -try: +from threading import Thread + import time + def create_s(): + sock = socket.socket() sock.bind(('', 80)) - print("Using port 80") -except OSError: - sock.bind(('', 8080)) - print("Using port 8080") - -sock.listen(5) - -conn, addr = sock.accept() -print("Connected", addr) - -data = conn.recv(8192) -msg = data.decode() - -print(msg) - -resp = """HTTP/1.1 200 OK -Server: SelfMadeServer v0.0.1 -Content-type: text/html -Connection: close - -Hello, webworld!""" - -conn.send(resp.encode()) - -conn.close() \ No newline at end of file + print("Сервер запущен") + sock.listen(5) + while True: + conn, addr = sock.accept() + thread = Thread(target=work, args=(conn,addr,)) + thread.start() + def work(conn, addr): + print("Присоединился", addr) + t = time.asctime(time.gmtime()).split(' ') + t = f'{t[0]}, {t[2]} {t[1]} {t[4]} {t[3]}' + print("Date: ",t) + h = f'HTTP/1.1 200 OK\nServer: SelfMadeServer v0.0.1\nDate: {t}\nContent-Type: text/html; charset=utf-8\nConnection: close\n\n' + user = conn.recv(1024).decode() + rez = user.split(" ")[1] + if rez=='/' or rez=='/index.html': + with open('index.html', 'rb') as f: + answer = f.read() + conn.send(h.encode('utf-8')+answer) + elif rez=="/1.html": + with open('1.html', 'rb') as f: + answer = f.read() + conn.send(h.encode('utf-8')+answer) + else: + resp = """HTTP/1.1 200 OK + + NOT FOUND""" + conn.send(resp.encode('utf-8')) + if __name__ == "__main__": + create_s() From d3a02512f12fbc0ae9dc84b3a28d944ae4d26942 Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 18 Nov 2021 15:04:59 +0300 Subject: [PATCH 2/7] Create 1.html --- 1.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1.html diff --git a/1.html b/1.html new file mode 100644 index 0000000..c5178bd --- /dev/null +++ b/1.html @@ -0,0 +1,15 @@ + + +
+ + + +