Bassically可以使用BaseHTTPServer这样并运行代码。
localhost.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()
home.html的
<html>
<head>
<title>Python | Home</title>
</head>
<body>
%(message)s
</body>
</html>
如果你在浏览器中打开http://localhost:8654
你:
this is how simple templating works
基本上你不能。使用一些框架,如我们Django,瓶等 – nu11p01n73R 2014-10-10 07:26:01
我主要关注如何在浏览器中运行脚本,并感谢您的时间@ nu11p01n73R – 2014-10-10 07:27:31
为什么downvote downvoters – 2014-10-10 07:27:53