我有一個在Python中實現的非常簡單的HTTPServer。代碼如下:強制重載在Python中的SimpleHTTP服務器上
import SimpleHTTPServer
import SocketServer as socketserver
import os
import threading
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
path_to_image = 'RGBWebcam1.png'
img = open(path_to_image, 'rb')
statinfo = os.stat(path_to_image)
img_size = statinfo.st_size
print(img_size)
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "image/png")
self.send_header("Content-length", img_size)
self.end_headers()
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "image/png")
self.send_header("Content-length", img_size)
self.end_headers()
f = open(path_to_image, 'rb')
self.wfile.write(f.read())
f.close()
class MyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
def __init__(self, server_adress, RequestHandlerClass):
self.allow_reuse_address = True
socketserver.TCPServer.__init__(self, server_adress, RequestHandlerClass, False)
if __name__ == "__main__":
HOST, PORT = "192.168.2.10", 9999
server = MyServer((HOST, PORT), MyHandler)
server.server_bind()
server.server_activate()
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
while(1):
print "test"
如果我連接到給定的IP地址加載頁面,一切都很好。現在如果頁面每隔n秒會自動刷新一次會很好。 我對python非常陌生,尤其是web編碼新手。我發現LiveReload但是我無法理解我如何將這兩個庫合併在一起。
謝謝您的幫助
你不能強迫瀏覽器從服務器端重新加載頁面。這隻有在客戶端纔有可能。 livereload使用一些實現重新加載的JavaScript修補了所提供的頁面:[https://github.com/lepture/python-livereload/blob/master/livereload/server.py#L222] – languitar