From 54e90b9d61bed3537c2023bc2a5d7d1c5c724735 Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Sun, 21 Nov 2021 21:57:09 +0300 Subject: [PATCH 1/8] Create index.html --- index.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..932fe54 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

Первый файл

+ + From 4b49696c9c92f018c8c4479776394e44a7cf4427 Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Sun, 21 Nov 2021 21:57:40 +0300 Subject: [PATCH 2/8] Create 2.html --- 2.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 2.html diff --git a/2.html b/2.html new file mode 100644 index 0000000..0edce3c --- /dev/null +++ b/2.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

Второй файл

+ + From 6ac68672adcabb65bb6ed3977be16798bc97d7c5 Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Sun, 21 Nov 2021 21:58:41 +0300 Subject: [PATCH 3/8] Update server.py --- server.py | 64 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/server.py b/server.py index 9ed7429..5316c18 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,55 @@ import socket +from threading import Thread +from datetime import datetime -sock = socket.socket() +SERVER_HOST = '127.0.0.1' +SERVER_PORT = 80 -try: - sock.bind(('', 80)) - print("Using port 80") -except OSError: - sock.bind(('', 8080)) - print("Using port 8080") -sock.listen(5) +def handle_request(client_connection, client_address): -conn, addr = sock.accept() -print("Connected", addr) + request = client_connection.recv(8192).decode() + print(f'Request: \n{request}') -data = conn.recv(8192) -msg = data.decode() + headers = request.split('\n') + filename = headers[0].split()[1] + if filename == '/': + filename = 'index.html' -print(msg) + try: + page = open('pages/' + filename) + content = page.read() + page.close() -resp = """HTTP/1.1 200 OK -Server: SelfMadeServer v0.0.1 -Content-type: text/html -Connection: close + headers = f'HTTP/1.1 200 OK\n\ + Server: SelfMadeServer v0.0.1\n\ + Date: {datetime.now()}\n\ + Content-Type: text/html; charset=utf-8\n\ + Connection: close\n\n' -Hello, webworld!""" + response = headers + content + except FileNotFoundError: + response = 'HTTP/1.0 404 NOT FOUND\n\nFile Not Found' -conn.send(resp.encode()) + client_connection.sendall(response.encode()) -conn.close() \ No newline at end of file + +def main(): + + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server_socket.bind((SERVER_HOST, SERVER_PORT)) + + + print(f'Listening on port {SERVER_PORT}') + server_socket.listen(5) + + client_connection, client_address = server_socket.accept() + + user_thread = Thread(target=handle_request, args=(client_connection, client_address)) + user_thread.start() + + server_socket.close() + + +if __name__ == '__main__': + main() From 278f9ba2429582661945522fb6b759047cef3fbf Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Wed, 8 Dec 2021 14:59:30 +0300 Subject: [PATCH 4/8] Update server.py --- server.py | 161 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 107 insertions(+), 54 deletions(-) diff --git a/server.py b/server.py index 5316c18..4b3bf04 100644 --- a/server.py +++ b/server.py @@ -1,55 +1,108 @@ +import datetime as d +import os +from contextlib import closing import socket -from threading import Thread -from datetime import datetime - -SERVER_HOST = '127.0.0.1' -SERVER_PORT = 80 - - -def handle_request(client_connection, client_address): - - request = client_connection.recv(8192).decode() - print(f'Request: \n{request}') - - headers = request.split('\n') - filename = headers[0].split()[1] - if filename == '/': - filename = 'index.html' - - try: - page = open('pages/' + filename) - content = page.read() - page.close() - - headers = f'HTTP/1.1 200 OK\n\ - Server: SelfMadeServer v0.0.1\n\ - Date: {datetime.now()}\n\ - Content-Type: text/html; charset=utf-8\n\ - Connection: close\n\n' - - response = headers + content - except FileNotFoundError: - response = 'HTTP/1.0 404 NOT FOUND\n\nFile Not Found' - - client_connection.sendall(response.encode()) - - -def main(): - - server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - server_socket.bind((SERVER_HOST, SERVER_PORT)) - - - print(f'Listening on port {SERVER_PORT}') - server_socket.listen(5) - - client_connection, client_address = server_socket.accept() - - user_thread = Thread(target=handle_request, args=(client_connection, client_address)) - user_thread.start() - - server_socket.close() - - -if __name__ == '__main__': - main() +import threading + +def logging (name, errorc, date, ipe):#, nom + f = open(log_nam, 'a') + f.write(date+' '+ipe+' '+name+' '+errorc+'\n') + f.close() +def find_free_port(nastroip): + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + s.bind((nastroip, 0)) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + return s.getsockname()[1] +def file_reader(name): + file = open(name, 'r') + content = file.read() + file.close() + return content + +nastroi=[] +with open ('nastroy.txt', 'r') as f: + for i in f: + nastroi.append(i.split(': ')[1][:-1]) +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +nastroi[0]=int(nastroi[0]) +nastroi[2]=int(nastroi[2]) +try: + sock.bind((nastroi[1], nastroi[0])) + print(f"Ваш адрес {nastroi[1]} {nastroi[0]}") +except OSError: + nastroi[0] = find_free_port(nastroi[1]) + print('Ошибка. Выбранный код сервера занят, код сервера будет изменён автоматически. Новый код: ',nastroi[1], nastroi[0]) + sock.bind(('', nastroi[0])) +nastroi[0]=str(nastroi[0]) +log_nam = 'log_server'+nastroi[1]+'_'+str(nastroi[0])+'.txt' +date=str(d.datetime.date(d.datetime.today())) +try: + with open (log_nam, 'a') as f: + f.write(date+' '+nastroi[1]+' '+nastroi[0]+' admin Server activate\n') +except FileNotFoundError: + with open (log_nam, 'w') as f: + f.write(date+' '+nastroi[1]+' '+nastroi[0]+' admin Server activate\n') +sock.listen(1) + +class ConnectionW(threading.Thread): + def __init__(self, conn, addr, direct): + super().__init__(daemon=True) + self.conn = conn + self.addr = addr + self.ipe = str(addr) + self.hello() + self.direct=direct + self.work_sun_is_high() + + def hello(self): + print("Connected", self.addr) + def work_sun_is_high(self): + while True: + try: + fl = self.conn.makefile('r') + except ConnectionAbortedError: + break + msg = fl.readline(nastroi[2]).split()[1] + if msg == '/': + msg='/index.html' + typfi = 'html' + else: + try: + typfi=msg.split('.')[-1] + except IndexError: + typfi='-' + fl=os.path.join(self.direct, msg[1:]) + if os.path.exists(fl) == True: + if typfi == 'txt' or typfi == 'html': + typfi='text/html' + self.otprav(file_reader(fl), msg, typfi, 'non_error') + '''elif typfi == 'img':#''' + + '''ошибка 403''' + else: + self.otprav("Error 403. The format is not supported", msg, typfi, '403') + else: + self.otprav("Error 404. File not found", msg, typfi, '404')#''' + + self.conn.close() + def otprav(self, resp, msg, typfi, err): + date = str(d.datetime.date(d.datetime.today())) + logging(msg, err, date, self.ipe) + ln_res=len(resp) + resp = f"""HTTP/1.1 200 OK +Server: SelfMadeServer v0.0.1 +Content-type: {typfi} +Date: {date} +Connection: close +Date: {date} +Content-length: {ln_res} +{resp}""" + self.conn.send(resp.encode('utf-8')) + +direct=os.getcwd() +while True: + try: + potok = ConnectionW(*sock.accept(), direct) + potok.start() + except: + continue From 3138798561efc80788d5ce1aed9448b2584ae673 Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Wed, 8 Dec 2021 15:01:33 +0300 Subject: [PATCH 5/8] Update index.html --- index.html | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 932fe54..26357d2 100644 --- a/index.html +++ b/index.html @@ -1,12 +1,9 @@ - + - - - - Document + Welcome index.html -

Первый файл

+

Welcome new page

From d1beca43caefba42fc22e21e0db7940278ed72d3 Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Wed, 8 Dec 2021 15:02:55 +0300 Subject: [PATCH 6/8] Create log_server127.0.0.1_8080.txt --- log_server127.0.0.1_8080.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 log_server127.0.0.1_8080.txt diff --git a/log_server127.0.0.1_8080.txt b/log_server127.0.0.1_8080.txt new file mode 100644 index 0000000..2ce162a --- /dev/null +++ b/log_server127.0.0.1_8080.txt @@ -0,0 +1,5 @@ +2021-11-19 127.0.0.1 8080 admin Server activate +2021-11-19 ('127.0.0.1', 55723) /index.html non_error +2021-11-19 ('127.0.0.1', 55724) /index.html non_error +2021-11-19 ('127.0.0.1', 55752) /index.html non_error +2021-11-19 ('127.0.0.1', 55781) /index.html non_error From 21c902a63bb3664a9c8306af112ce6cf58f1a07a Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Wed, 8 Dec 2021 15:03:15 +0300 Subject: [PATCH 7/8] Create nastroy.txt --- nastroy.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 nastroy.txt diff --git a/nastroy.txt b/nastroy.txt new file mode 100644 index 0000000..b54d2e4 --- /dev/null +++ b/nastroy.txt @@ -0,0 +1,3 @@ +port: 8080 +directoria: 127.0.0.1 +max_b: 4096 From 5adafa6c24c2256cf9256ea4608d369a02ccd212 Mon Sep 17 00:00:00 2001 From: anna01122002 <76559002+anna01122002@users.noreply.github.com> Date: Wed, 8 Dec 2021 15:03:52 +0300 Subject: [PATCH 8/8] Create page.html --- page.html | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 page.html diff --git a/page.html b/page.html new file mode 100644 index 0000000..9f9bc37 --- /dev/null +++ b/page.html @@ -0,0 +1,9 @@ + + + + New Page + + +

It is inner page

+ +