Skip to content

Макаров М.И. #67

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 2 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 404.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 http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>error</h1>
</body>
</html>
Binary file added img_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions index.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 http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Document1</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions index2.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 http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document2</title>
</head>
<body>
<h1>Document2</h1>
</body>
</html>
14 changes: 14 additions & 0 deletions index3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document3</title>
</head>
<body>
<h1>
Document3
</h1>
</body>
</html>
76 changes: 56 additions & 20 deletions server.py
Original file line number Diff line number Diff line change
@@ -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()
if __name__=='__main__':
server_starter()