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 diff --git a/index.html b/index.html new file mode 100644 index 0000000..2b0b3c4 --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ + + + + + Title + + +

Страница index.html

+

Go to 1.html

+ + diff --git a/server.py b/server.py index 9ed7429..749c35a 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,38 @@ import socket - -sock = socket.socket() - -try: +from threading import Thread +import time +def create_server(): + 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("Server is on") + sock.listen(5) + while True: + conn, addr = sock.accept() + thread = Thread(target=work, args=(conn, addr,)) + thread.start() + + +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() + 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_server()