From 570e8b11f410d18f59083736cb35422491239b06 Mon Sep 17 00:00:00 2001 From: EdemGit <92623671+EdemGit@users.noreply.github.com> Date: Thu, 23 Dec 2021 08:07:59 +0300 Subject: [PATCH 1/2] Update server.py --- server.py | 77 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/server.py b/server.py index 9ed7429..2ec30e4 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,48 @@ import socket - -sock = socket.socket() - -try: - 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 +import time +from threading import Thread + + +def start_server(): + print('Starting the server...') + sock = socket.socket() + try: + sock.bind(('', 80)) + print('Using port 80') + except OSError: + sock.bind(('', 8080)) + print('Using port 8080') + sock.listen(5) + print('The server has launched') + while True: + conn, addr = sock.accept() + thread = Thread(target=proc, args=(conn, addr)) + thread.start() + + +filename = 'index.html' + + +def proc(conn, addr): + h = (f'HTTP/1.1 200 OK\n' + f'Server: Apache/2.2.17\n' + f'Date: {time.asctime()}\n' + f'Content-Type: text/html\n' + f'Connection: close\n\n') + try: + user = conn.recv(1024).decode() + print(user) + path = user.split(' ')[1] + if path == '/': + with open(filename, 'rb') as f: + conn.send(h.encode('utf-8') + f.read()) + else: + conn.send('HTTP/1.1 404\nNOT FOUND'.encode('utf-8')) + except IndexError: + conn.send('HTTP/1.1 404\nNOT FOUND'.encode('utf-8')) + print(addr, 'has connected') + + +if __name__ == '__main__': + filename = input('Enter the path (absolute or relative) to an existing .html file: ') + start_server() From 6bf6745f37fabc16a6a669693e1f2ac398531902 Mon Sep 17 00:00:00 2001 From: EdemGit <92623671+EdemGit@users.noreply.github.com> Date: Thu, 23 Dec 2021 08:08:22 +0300 Subject: [PATCH 2/2] Create index.html --- index.html | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..418eeb5 --- /dev/null +++ b/index.html @@ -0,0 +1,20 @@ + + +Edem's GitHub link + +
+
+

This is an example web page for the Web Server task.

+
+ + +

+

My GitHub page

+

+ +
+

Completed by Edem Karachik

+
+
+ +