from http.server import BaseHTTPRequestHandler, HTTPServer
from xml.dom import minidom
import os
class MyHandler(BaseHTTPRequestHandler):
def do_POST(self):
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.send_response(200)
self.end_headers()
valore = str(self.data_string)[2:-1]
response = ["",""]
response[0],response[1] = processData(valore)
if response[0] == 1:
sep = ""
message = ""
for res in response[1]:
message += res
response = sep.join(message)
self.wfile.write(bytes(message, "utf-8"))
def do_GET(self):
# Send response status code
self.send_response(200)
# Send headers
if self.path.endswith("html"):
self.send_header('Content-type', 'text/html')
self.end_headers()
elif self.path.endswith("css"):
self.send_header('Content-type', 'text/css')
self.end_headers()
elif self.path.endswith("js"):
self.send_header('Content-type', 'application/javascript')
self.end_headers()
elif self.path.endswith(".ico"):
print(self.rawpath)
if not self.path.endswith("jpeg") and not self.path.endswith("jpg") and not self.path.endswith("png") and not self.path.endswith("gif"):
with open(self.path[1:], 'r') as myfile:
data = myfile.read()
# Write content as utf-8 data
self.wfile.write(bytes(data, "utf8"))
if self.path.endswith("jpeg"):
f = open(self.path[1:], 'rb')
self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
self.wfile.write(f.read())
f.close()
elif self.path.endswith("png"):
f = open(self.path[1:], 'rb')
self.send_response(200)
self.send_header('Content-type', 'image/png')
self.end_headers()
self.wfile.write(f.read())
f.close()
elif self.path.endswith("gif"):
f = open(self.path[1:], 'rb')
self.send_response(200)
self.send_header('Content-type', 'image/gif')
self.end_headers()
self.wfile.write(f.read())
f.close()
elif self.path.endswith("jpg"):
f = open(self.path[1:], 'rb')
self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
def run():
print('starting server...')
# Server settings
# Choose port 8080, for port 80, which is normally used for a http server, you need root access
server_address = ('192.168.2.245', 8081)
httpd = HTTPServer(server_address, MyHandler)
print('running server...')
httpd.serve_forever()
def processData(data):
XMLlist = []
data = data.split(":")
if data[0] == "XMLlist":
path = os.path.realpath(__file__)
path = os.path.dirname(path)
for filename in os.listdir(path):
if filename.endswith('.xml'):
XMLlist.append(filename[:-3])
return 1,XMLlist
#fullname = os.path.join(path, filename)
else:
file = open('default.xml', 'r')
tree = minidom.parse(file)
tree.getElementsByTagName(data[0])[0].childNodes[0].replaceWholeText(data[1])
file = open('default.xml', 'w')
tree.writexml(file)
file.close()
return 0, 1
run()
Сервер работает нормально, и мне нужно удалить кеш. Когда я пытаюсь обновить изображение со страницы html с тем же именем, сервер возвращает мне кэшированное изображение...
Я нашел этот сервер в Интернете и немного отредактировал его: [code]from http.server import BaseHTTPRequestHandler, HTTPServer from xml.dom import minidom import os
class MyHandler(BaseHTTPRequestHandler): def do_POST(self): self.data_string = self.rfile.read(int(self.headers['Content-Length'])) self.send_response(200) self.end_headers() valore = str(self.data_string)[2:-1] response = ["",""] response[0],response[1] = processData(valore) if response[0] == 1: sep = "" message = "" for res in response[1]: message += res response = sep.join(message) self.wfile.write(bytes(message, "utf-8"))
def do_GET(self): # Send response status code self.send_response(200) # Send headers if self.path.endswith("html"): self.send_header('Content-type', 'text/html') self.end_headers() elif self.path.endswith("css"): self.send_header('Content-type', 'text/css') self.end_headers() elif self.path.endswith("js"): self.send_header('Content-type', 'application/javascript') self.end_headers() elif self.path.endswith(".ico"): print(self.rawpath) if not self.path.endswith("jpeg") and not self.path.endswith("jpg") and not self.path.endswith("png") and not self.path.endswith("gif"): with open(self.path[1:], 'r') as myfile: data = myfile.read() # Write content as utf-8 data self.wfile.write(bytes(data, "utf8")) if self.path.endswith("jpeg"): f = open(self.path[1:], 'rb') self.send_response(200) self.send_header('Content-type', 'image/jpeg') self.end_headers() self.wfile.write(f.read()) f.close() elif self.path.endswith("png"): f = open(self.path[1:], 'rb') self.send_response(200) self.send_header('Content-type', 'image/png') self.end_headers() self.wfile.write(f.read()) f.close() elif self.path.endswith("gif"): f = open(self.path[1:], 'rb') self.send_response(200) self.send_header('Content-type', 'image/gif') self.end_headers() self.wfile.write(f.read()) f.close() elif self.path.endswith("jpg"): f = open(self.path[1:], 'rb') self.send_response(200) self.send_header('Content-type', 'image/jpeg') self.end_headers() self.wfile.write(f.read()) f.close() return
def run(): print('starting server...')
# Server settings # Choose port 8080, for port 80, which is normally used for a http server, you need root access server_address = ('192.168.2.245', 8081) httpd = HTTPServer(server_address, MyHandler) print('running server...') httpd.serve_forever()
def processData(data): XMLlist = [] data = data.split(":") if data[0] == "XMLlist": path = os.path.realpath(__file__) path = os.path.dirname(path) for filename in os.listdir(path): if filename.endswith('.xml'): XMLlist.append(filename[:-3]) return 1,XMLlist #fullname = os.path.join(path, filename) else: file = open('default.xml', 'r') tree = minidom.parse(file) tree.getElementsByTagName(data[0])[0].childNodes[0].replaceWholeText(data[1]) file = open('default.xml', 'w') tree.writexml(file) file.close() return 0, 1
run() [/code] Сервер работает нормально, и мне нужно удалить кеш. Когда я пытаюсь обновить изображение со страницы html с тем же именем, сервер возвращает мне кэшированное изображение...