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 @@ + + + + + + + 1.html + + + +

Страница 1.html

+

Go to index.html

+ + + From f0e06f7e03ab64d21fd1dd51ebebe30ba9612482 Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 18 Nov 2021 15:05:37 +0300 Subject: [PATCH 3/7] Create index.html --- index.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..23fec8a --- /dev/null +++ b/index.html @@ -0,0 +1,15 @@ + + + + + + + index.html + + + +

Страница index.html

+

Go to 1.html

+ + + From 54ec83b1847e3a38d6df4f7ef406d3598a0aa730 Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 25 Nov 2021 14:18:42 +0300 Subject: [PATCH 4/7] Update server.py --- server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.py b/server.py index 7026a82..4ffa529 100644 --- a/server.py +++ b/server.py @@ -1,7 +1,7 @@ import socket from threading import Thread - import time +import time def create_s(): sock = socket.socket() sock.bind(('', 80)) From 8a8e98907cbd68a6ca44f336f5ef535f8bd29b74 Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 25 Nov 2021 15:11:14 +0300 Subject: [PATCH 5/7] Update index.html --- index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 23fec8a..04fdc47 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,7 @@

Страница index.html

-

Go to 1.html

+

Перейти на страницу 1.html

- + From 52243f4683cfdd806dca80c18bca55a2bbd26ea2 Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 25 Nov 2021 15:11:44 +0300 Subject: [PATCH 6/7] Update 1.html --- 1.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1.html b/1.html index c5178bd..68b4ef8 100644 --- a/1.html +++ b/1.html @@ -9,7 +9,7 @@

Страница 1.html

-

Go to index.html

+

Перейти на страницу index.html

- + From d14d5e3cc8895bfc3c75368123311976b2ba7b8a Mon Sep 17 00:00:00 2001 From: polina204539 <90207185+polina204539@users.noreply.github.com> Date: Thu, 25 Nov 2021 15:12:59 +0300 Subject: [PATCH 7/7] Update server.py --- server.py | 56 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/server.py b/server.py index 4ffa529..d791ce5 100644 --- a/server.py +++ b/server.py @@ -2,35 +2,33 @@ from threading import Thread import time - def create_s(): +def generate_function(): sock = socket.socket() sock.bind(('', 80)) 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() + sock.listen(5) + while True: + conn, addr = sock.accept() + thread = Thread(target=working, args=(conn,addr,)) + thread.start() +def working(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')) +generate_function()