Версия 03

This commit is contained in:
Антон Перегудин 2024-04-15 21:55:09 +03:00
parent dfe58749c2
commit a772870ab1
3 changed files with 33 additions and 7 deletions

13
index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
<p>Today is a beautiful day!</p>
<p>Feel free to explore.</p>
<p>Visit our <a href="/page1">Page 1</a>.</p>
<img src="https://toppng.com/uploads/preview/evidence-300x300-golden-baby-11564146233t27ef46tra.png" alt="Placeholder Image">
</body>
</html>

View File

@ -1,7 +1,7 @@
import socket
HOST = 'localhost'
PORT = 8081
PORT = 8080
def http_client():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:

View File

@ -1,7 +1,7 @@
import socket
PORT = 8081
HOST = '0.0.0.0'
PORT = 8080
HOST = 'localhost'
def http_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
@ -14,11 +14,24 @@ def http_server():
conn, addr = server.accept()
with conn:
print('Connected by', addr)
request = conn.recv(1024)
print('Request:', request.decode('utf-8'))
request = conn.recv(1024).decode('utf-8')
print('Request:', request)
method, path, *_ = request.split()
if method == 'GET':
if path == '/':
with open('index.html', 'rb') as file:
content = file.read()
response = b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n' + content
elif path == '/page1':
response = b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nThis is page 1'
else:
response = b'HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\nPage not found'
else:
response = b'HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/html\r\n\r\nMethod not allowed'
response = b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello, world'
conn.sendall(response)
if __name__ == '__main__':
http_server()
http_server()