我已经编写了一个TCP服务器,它与只包含文本的网页完美配合,但我似乎无法发送包含图片的HTML文件。我该怎么办呢?
DIR = "D:/wwwroot/"
SERVER = ("server_address", 8820) #server_address replaced with my IP
PHRASE_CHART = "D:/wwwroot/phrase_chart.txt"
CONTENT_TYPE_CHART = "D:/wwwroot/content_type_chart.txt"
DEFAULT_PAGE = "D:/wwwroot/main.html"
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(SERVER)
client_counter = 0
while client_counter <= 10:
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
client_counter += 1
while True:
try:
request = client_socket.recv(1024)
request = request[0:request.find("/r/n/r/n")]
if request[0:3] == "GET":
file_name = request[5: request.find("HTTP/1.1") - 1]
if os.path.isfile(DIR + file_name):
page = open(DIR + file_name)
data = ""
for line in page:
data += line
client_socket.send(data)
elif file_name == "":
page = open(DEFAULT_PAGE)
data = ""
for line in page:
data += line
client_socket.send(data)
else:
client_socket.send("")
else:
client_socket.close()
except socket.error:
break
client_socket.close()
server_socket.close()