Reputation: 13
I have a simple Web Server made using Python Socket. This server has only two file, 'home.html' and 'home.css' in a templates folder. But when I visit home.html I see no effect by my home.css file. I checked Microsoft Edge's DevTools and It showed a black home.css file.
My Folders:
Website
- server.py
- templates
- home.html
- home.css
home.html code:
<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<link rel="stylesheet" type="text/css" href="home.css">
</head>
<body>
<div class="header">
<h1>title name</h1>
</div>
<div class="body_container">
<p>This is body..</p>
</div>
</body>
</html
home.css code:
body{
background-color: gray;
}
server.py:
import time
import socket
import threading
class WebServer():
def __init__(self):
self.host = "192.168.0.101"
self.port = 80
self.content_dir = 'templates'
def _generate_headers(self, response_code):
header = ''
if response_code == 200:
header += 'HTTP/1.1 200 OK\n'
elif response_code == 404:
header += 'HTTP/1.1 404 Not Found\n'
time_now = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
header += 'Date: {now}\n'.format(now=time_now)
header += 'Server: Orasae\n'
header += 'Connection: close\n\n' # Signal that connection will be closed after completing the request
return header
def handle_client(self, conn, addr):
"""
Handles Client Connection and Serves Files From self.content_dir.
parameters:
- conn: Client Socket Object from self.server.accept();
- addr: Client Address from self.server.accept();
"""
PACKET_SIZE = 1024
CONNECTION = True
while CONNECTION:
data = conn.recv(PACKET_SIZE).decode() # Receive Data From Client and Decode it.
if not data:
CONNECTION = False
response = self._generate_headers(200).encode()
directory = data.split(" ")[1] # gets directory for requested file
if directory == '/' or directory == '/home':
f = open(f"{self.content_dir}/home.html", 'rb')
htmlCode = f.read()
f.close()
response += htmlCode
conn.send(response)
conn.close()
CONNECTION = False
def _listen(self):
"""
Listens For Any Connection on self.port and Calls self.handle_client For
Handling Client Request/Respond.
"""
self.server.listen(1)
while True:
conn, addr = self.server.accept()
threading.Thread(target=self.handle_client, args=(conn, addr)).start()
def start(self):
"""
Attempts to create and bind to a socket to launch it.
"""
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print(f"Starting Server on {self.host}:{self.port}")
self.server.bind((self.host, self.port))
print(f"Started Server on {self.host}:{self.port}")
except:
print(f"[ERROR] Couldn't Start Server on port {self.port}")
self._listen()
Thanks for your Help!
Upvotes: 1
Views: 204
Reputation: 1839
Your server is never reading home.css
file. When client requests you need to parse the request and find the file client is requesting for, and deliver it accordingly. By the way, HTTP line delimiters are \r\n
.
Upvotes: 1
Reputation: 21
try like this:
html,body{background-color:#ccc}
OR just testing purpose try
<body bgcolor="black">
Upvotes: 1