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
+
+
+ Второй файл
+
+
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..26357d2
--- /dev/null
+++ b/index.html
@@ -0,0 +1,9 @@
+
+
+
+ Welcome index.html
+
+
+ Welcome new page
+
+
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
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
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
+
+
diff --git a/server.py b/server.py
index 9ed7429..4b3bf04 100644
--- a/server.py
+++ b/server.py
@@ -1,31 +1,108 @@
+import datetime as d
+import os
+from contextlib import closing
import socket
+import threading
-sock = socket.socket()
+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(('', 80))
- print("Using port 80")
+ sock.bind((nastroi[1], nastroi[0]))
+ print(f"Ваш адрес {nastroi[1]} {nastroi[0]}")
except OSError:
- sock.bind(('', 8080))
- print("Using port 8080")
-
-sock.listen(5)
+ 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)
-conn, addr = sock.accept()
-print("Connected", addr)
+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()
-data = conn.recv(8192)
-msg = data.decode()
+ 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':#'''
-print(msg)
+ '''ошибка 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')#'''
-resp = """HTTP/1.1 200 OK
+ 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: text/html
+Content-type: {typfi}
+Date: {date}
Connection: close
+Date: {date}
+Content-length: {ln_res}
+{resp}"""
+ self.conn.send(resp.encode('utf-8'))
-Hello, webworld!"""
-
-conn.send(resp.encode())
-
-conn.close()
\ No newline at end of file
+direct=os.getcwd()
+while True:
+ try:
+ potok = ConnectionW(*sock.accept(), direct)
+ potok.start()
+ except:
+ continue