diff --git a/index.html b/index.html new file mode 100644 index 0000000..652da93 --- /dev/null +++ b/index.html @@ -0,0 +1,13 @@ + + + + Home Page + + +

Welcome to My Website!

+

Today is a beautiful day!

+

Feel free to explore.

+

Visit our Page 1.

+ Placeholder Image + + diff --git a/socket_client.py b/socket_client.py index 6f0d618..f01426b 100644 --- a/socket_client.py +++ b/socket_client.py @@ -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: diff --git a/socket_server.py b/socket_server.py index a31d9d0..1ef6cfa 100644 --- a/socket_server.py +++ b/socket_server.py @@ -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() \ No newline at end of file + http_server()