I am making a httpServer. When the client enter localhost:8888/good.html
, he will be routed to the corresponding page.
What I get confused is that my css response(res_for_style
) keep pending. Please let me know where I got wrong.
Server.py
import socket
import json
import sys
import threading
pars = ('127.0.0.1', 8888)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(pars)
s.listen(5)
res_for_good = '''HTTP/1.1 200 OK
Content-Type: text/html
<head>
<link rel="stylesheet" href="./style.css" type="text/css">
</head>
<html>
<body>good HaHa</body>
</html>
'''
res_for_style='''HTTP/1.1 200 OK
Content-Type: text/css
body{
color: red;
}
'''
def serveClient(clientsocket, address):
while True:
data = clientsocket.recv(1024)
data_utf8=data.decode('utf-8').split('\r\n')
if '/good.html' in data_utf8[0]:
clientsocket.sendall(res_for_good.encode())
if '/style.css' in data_utf8[0]:
print("transfer css")
res="Content-Type: text/css\n\n"+css_file.read()
clientsocket.sendall(res_for_style.encode())
if data == b'':
clientsocket.close()
break
while True:
(clientsocket, address) = s.accept()
threading.Thread(target = serveClient, args = (clientsocket, address)).start()