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

+
+
+ + 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()