diff --git a/1.html b/1.html new file mode 100644 index 0000000..68b4ef8 --- /dev/null +++ b/1.html @@ -0,0 +1,15 @@ + + + + + + + 1.html + + + +

Страница 1.html

+

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

+ + + diff --git a/index.html b/index.html new file mode 100644 index 0000000..04fdc47 --- /dev/null +++ b/index.html @@ -0,0 +1,15 @@ + + + + + + + index.html + + + +

Страница index.html

+

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

+ + + diff --git a/server.py b/server.py index 9ed7429..d791ce5 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,34 @@ import socket -sock = socket.socket() - -try: +from threading import Thread +import time +def generate_function(): + 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=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()