diff --git a/404.html b/404.html new file mode 100644 index 0000000..d54ab91 --- /dev/null +++ b/404.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

error

+ + \ No newline at end of file diff --git a/img_1.jpg b/img_1.jpg new file mode 100644 index 0000000..9b5a496 Binary files /dev/null and b/img_1.jpg differ diff --git a/img_2.jpg b/img_2.jpg new file mode 100644 index 0000000..8da3eee Binary files /dev/null and b/img_2.jpg differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..2487845 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

Document1

+ + \ No newline at end of file diff --git a/index2.html b/index2.html new file mode 100644 index 0000000..da3fd1d --- /dev/null +++ b/index2.html @@ -0,0 +1,12 @@ + + + + + + + Document2 + + +

Document2

+ + \ No newline at end of file diff --git a/index3.html b/index3.html new file mode 100644 index 0000000..273b778 --- /dev/null +++ b/index3.html @@ -0,0 +1,14 @@ + + + + + + + Document3 + + +

+ Document3 +

+ + \ No newline at end of file diff --git a/server.py b/server.py index 9ed7429..9dc0513 100644 --- a/server.py +++ b/server.py @@ -1,31 +1,67 @@ import socket +import magic +from datetime import datetime sock = socket.socket() -try: - sock.bind(('', 80)) - print("Using port 80") -except OSError: - sock.bind(('', 8080)) - print("Using port 8080") +def locate(dec_inf, conn): + result=dec_inf.split('\n')[0].split()[1][1:] #вытаскиваем из запроса имя страницы + currentDate = datetime.now().strftime("%a, %d %b %Y %H:%M:%S GTM") + content_type = "text/html; charset=utf-8" + if not result: + result = "index.html" + try: + if result.split('.')[1] == ("jpeg", "jpg"): + mime = magic.Magic(mime=True) + content_type = mime.from_file(f"./{result}") + except: + pass + send_this=f'''HTTP/1.1 200 OK + Server: SelfMadeServer v0.0.1 + Date: {currentDate} + Content-Type: {content_type} + Connection: close\n\n''' -sock.listen(5) + send_if_error=f'''HTTP/1.1 400 ERROR + Server:SelfMadeServer v0.0.1 + Date: {currentDate} + Content-Type: multipart/form-data; charset=utf-8 + Connection: close \n\n''' + try: + with open(result,'rb') as file: + info=file.read() + to_encode=send_this.encode()+info + conn.send(to_encode) + except FileNotFoundError: + with open('404.html','rb') as err: + info=err.read() + to_encode=send_if_error.encode()+info + conn.send(to_encode) -conn, addr = sock.accept() -print("Connected", addr) -data = conn.recv(8192) -msg = data.decode() +def manipulator(conn): + inf=conn.recv(8192) # получаем запрос по 8кб + dec_inf=inf.decode() #переводим из битового представления + locate(dec_inf, conn) -print(msg) +def server_starter(): + try: + sock.bind(('', 80)) + print("Using port 80") + except OSError: + sock.bind(('', 8080)) + print("Using port 8080") -resp = """HTTP/1.1 200 OK -Server: SelfMadeServer v0.0.1 -Content-type: text/html -Connection: close + sock.listen(5) + while True: + try: + conn, addr = sock.accept() + print("Connected", addr) + manipulator(conn) -Hello, webworld!""" + except KeyboardInterrupt: + conn.close() + break -conn.send(resp.encode()) - -conn.close() \ No newline at end of file +if __name__=='__main__': + server_starter()