Skip to content
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
11 changes: 11 additions & 0 deletions 1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Страница 1.html</h1>
<p><a href="index.html">Go to index.html</a></p>
</body>
</html>
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Страница index.html</h1>
<p><a href="1.html">Go to 1.html</a></p>
</body>
</html>
65 changes: 36 additions & 29 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import socket

sock = socket.socket()

try:
from threading import Thread
import time
def create_server():
sock = socket.socket()
sock.bind(('', 80))
print("Using port 80")
except OSError:
sock.bind(('', 8080))
print("Using port 8080")

sock.listen(5)

conn, addr = sock.accept()
print("Connected", addr)

data = conn.recv(8192)
msg = data.decode()

print(msg)

resp = """HTTP/1.1 200 OK
Server: SelfMadeServer v0.0.1
Content-type: text/html
Connection: close

Hello, webworld!"""

conn.send(resp.encode())

conn.close()
print("Server is on")
sock.listen(5)
while True:
conn, addr = sock.accept()
thread = Thread(target=work, args=(conn, addr,))
thread.start()


def work(conn, addr):
print("Joined", addr)
t = time.asctime(time.gmtime()).split(' ')
t = f'{t[0]}, {t[2]} {t[1]} {t[4]} {t[3]}'
print("Date: ", t)
h = f'HTTP/1.1 200 OK\nServer: SelfMadeServer v0.0.1\nDate: {t}\nContent-Type: text/html; charset=utf-8\nConnection: close\n\n'
user = conn.recv(1024).decode()
rez = user.split(" ")[1]
if rez == '/' or rez == '/index.html':
with open('index.html', 'rb') as f:
answer = f.read()
conn.send(h.encode('utf-8') + answer)
elif rez == "/1.html":
with open('1.html', 'rb') as f:
answer = f.read()
conn.send(h.encode('utf-8') + answer)
else:
resp = """HTTP/1.1 200 OK
NOT FOUND"""
conn.send(resp.encode('utf-8'))


if __name__ == "__main__":
create_server()