基本上,您可以像这样使用BaseHTTPServer并运行代码。在
在本地主机.py在#!/usr/bin/env python
import BaseHTTPServer
class HTTPFrontend(object) :
def __init__(self, port) :
self.server = BaseHTTPServer.HTTPServer(('', port), self.RequestHandler)
print "Web interface listening on http://localhost:" + str(port)
def start(self) :
self.server.serve_forever()
def stop(self) :
self.server.socket.close()
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler) :
def do_GET(self) :
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
templateFile = open("home.html")
template = templateFile.read()
templateFile.close()
message = "this is how simple templating works"
self.wfile.write(template % {'message': message})
def do_POST(self) :
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write("this is a POST")
if __name__ == "__main__":
server = HTTPFrontend(8080)
server.start()
在主页.html在
^{pr2}$
如果您在浏览器中打开http://localhost:8654,您将得到:this is how simple templating works
基本上,您可以像这样使用BaseHTTPServer并运行代码。在
在本地主机.py在#!/usr/bin/env python
import BaseHTTPServer
class HTTPFrontend(object) :
def __init__(self, port) :
self.server = BaseHTTPServer.HTTPServer(('', port), self.RequestHandler)
print "Web interface listening on http://localhost:" + str(port)
def start(self) :
self.server.serve_forever()
def stop(self) :
self.server.socket.close()
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler) :
def do_GET(self) :
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
templateFile = open("home.html")
template = templateFile.read()
templateFile.close()
message = "this is how simple templating works"
self.wfile.write(template % {'message': message})
def do_POST(self) :
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write("this is a POST")
if __name__ == "__main__":
server = HTTPFrontend(8080)
server.start()
在主页.html在
^{pr2}$
如果您在浏览器中打开http://localhost:8654,您将得到:this is how simple templating works