我创建了一个“处理程序” python脚本,如下所示:
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Serving at port:", PORT
httpd.serve_forever()
我也有一个javascript函数,该函数将URL请求发送到服务器信息.一切正常,当我运行处理程序脚本,然后运行JavaScript时,可以在终端中查看来自JS的所有数据:
localhost.localadmin - - [20/Feb/2013] "GET /?var=data/ HTTP/1.1" 200 -
我想做的是,能够从我的处理程序脚本访问此数据,理想情况下是这样的:
data = httpd.server_forever() #How do I do somethign like this
我该怎么做?
解决方法:
您可以像这样继承SimpleHTTPServer.SimpleHTTPRequestHandler:
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# Your code here
print "Client requested:", self.command, self.path
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
PORT = 8000
httpd = SocketServer.TCPServer(("", PORT), MyHandler)
print "Serving at port:", PORT
httpd.serve_forever()
这将在控制台中打印:
Client requested GET /?var=data/
有关更多信息,请参阅SimpleHTTPRequestHandler和BaseHTTPRequestHandler上的文档.