Skip to content

Быкадорова ПИ0-1 #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Второй файл</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html lang="en">
<head>
<title>Welcome index.html</title>
</head>
<body>
<h4>Welcome new page</h4>
</body>
</html>
5 changes: 5 additions & 0 deletions log_server127.0.0.1_8080.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions nastroy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
port: 8080
directoria: 127.0.0.1
max_b: 4096
9 changes: 9 additions & 0 deletions page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html lang="en">
<head>
<title>New Page</title>
</head>
<body>
<h3>It is inner page</h3>
</body>
</html>
115 changes: 96 additions & 19 deletions server.py
Original file line number Diff line number Diff line change
@@ -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()
direct=os.getcwd()
while True:
try:
potok = ConnectionW(*sock.accept(), direct)
potok.start()
except:
continue