From 2d068f7b55afbde8cbe6ed417d3e4480347b43d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=80=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= <90414850+6Nkita9@users.noreply.github.com> Date: Wed, 24 Nov 2021 15:38:20 +0300 Subject: [PATCH 1/6] Update server.py --- server.py | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/server.py b/server.py index 9ed7429..b1d95cf 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,35 @@ import socket - +from threading import Thread + +def working(conn, addr): + print(f'{addr} was connected') + + user = conn.recv(1024).decode() + print(user) + request_from_server = user.split(" ")[1] + if request_from_server == '/': + with open('index.html', 'rb') as file: + site = file.read() + conn.send(site) + if request_from_server == '/index.html': + with open('index.html', 'rb') as file: + site = file.read() + conn.send(site) + + else: + with open('error.html', 'rb') as file: + site = file.read() + conn.send(site) + + sock = socket.socket() - try: sock.bind(('', 80)) - print("Using port 80") except OSError: sock.bind(('', 8080)) - print("Using port 8080") - +print("Сервер запущен") 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 +while True: + conn, addr = sock.accept() + thread = Thread(target=working, args=(conn, addr,)) + thread.start() From 90916eb97b803ed2171b0c2552c6745d92424e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=80=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= <90414850+6Nkita9@users.noreply.github.com> Date: Wed, 24 Nov 2021 15:40:14 +0300 Subject: [PATCH 2/6] Add files via upload --- error.html | 15 +++++++++++++++ index.html | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 error.html create mode 100644 index.html diff --git a/error.html b/error.html new file mode 100644 index 0000000..7a4737f --- /dev/null +++ b/error.html @@ -0,0 +1,15 @@ + + + + + + + 1.html + + + +

Ошибка, такой страницы нет, можете зайти на главную

+

Стандартная страница

+ + + \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..a32bf6b --- /dev/null +++ b/index.html @@ -0,0 +1,16 @@ + + + + + + + 1.html + + + +

Тестовая страница html

+

Главная страница

+ + + + \ No newline at end of file From 45bc9d190d57b57bc2dbe165096d441869e20a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=80=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= <90414850+6Nkita9@users.noreply.github.com> Date: Thu, 25 Nov 2021 23:40:34 +0300 Subject: [PATCH 3/6] Delete error.html --- error.html | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 error.html diff --git a/error.html b/error.html deleted file mode 100644 index 7a4737f..0000000 --- a/error.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - 1.html - - - -

Ошибка, такой страницы нет, можете зайти на главную

-

Стандартная страница

- - - \ No newline at end of file From d11ae64ce8e4cdf19aef0c236ab5d9a8a7f04939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=80=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= <90414850+6Nkita9@users.noreply.github.com> Date: Thu, 25 Nov 2021 23:41:38 +0300 Subject: [PATCH 4/6] Add files via upload --- 1.html | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1.html diff --git a/1.html b/1.html new file mode 100644 index 0000000..2104597 --- /dev/null +++ b/1.html @@ -0,0 +1,11 @@ + + + + + Title + + +

Страница 1.html

+

Go to index.html

+ + \ No newline at end of file From 1cfb0b7c8d867a30d27223c3b8f0c6adba1609c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=80=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= <90414850+6Nkita9@users.noreply.github.com> Date: Thu, 25 Nov 2021 23:42:01 +0300 Subject: [PATCH 5/6] Update index.html --- index.html | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index a32bf6b..2b0b3c4 100644 --- a/index.html +++ b/index.html @@ -1,16 +1,11 @@ - + - - - 1.html + Title - -

Тестовая страница html

-

Главная страница

- +

Страница index.html

+

Go to 1.html

- - \ No newline at end of file + From 76aa3d905ed62065d786fafee4624b4ed52c1eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=80=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= <90414850+6Nkita9@users.noreply.github.com> Date: Thu, 25 Nov 2021 23:42:53 +0300 Subject: [PATCH 6/6] Update server.py --- server.py | 59 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/server.py b/server.py index b1d95cf..749c35a 100644 --- a/server.py +++ b/server.py @@ -1,35 +1,38 @@ import socket from threading import Thread +import time +def create_server(): + sock = socket.socket() + sock.bind(('', 80)) + print("Server is on") + sock.listen(5) + while True: + conn, addr = sock.accept() + thread = Thread(target=work, args=(conn, addr,)) + thread.start() -def working(conn, addr): - print(f'{addr} was connected') +def work(conn, addr): + print("Joined", 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() - print(user) - request_from_server = user.split(" ")[1] - if request_from_server == '/': - with open('index.html', 'rb') as file: - site = file.read() - conn.send(site) - if request_from_server == '/index.html': - with open('index.html', 'rb') as file: - site = file.read() - conn.send(site) - + 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: - with open('error.html', 'rb') as file: - site = file.read() - conn.send(site) + resp = """HTTP/1.1 200 OK + NOT FOUND""" + conn.send(resp.encode('utf-8')) - -sock = socket.socket() -try: - sock.bind(('', 80)) -except OSError: - sock.bind(('', 8080)) -print("Сервер запущен") -sock.listen(5) -while True: - conn, addr = sock.accept() - thread = Thread(target=working, args=(conn, addr,)) - thread.start() + +if __name__ == "__main__": + create_server()