Python 在Django中使用SimpleXMLRPCDispatcher时如何获取IP
其代码源于: 如何在“post_log”定义中获取客户端的IP地址? 我见过,但不能把它应用到我的案例中Python 在Django中使用SimpleXMLRPCDispatcher时如何获取IP,python,django,ip-address,xml-rpc,simplexmlrpcserver,Python,Django,Ip Address,Xml Rpc,Simplexmlrpcserver,其代码源于: 如何在“post_log”定义中获取客户端的IP地址? 我见过,但不能把它应用到我的案例中 谢谢。好的,我可以。。。有一些妙招 首先,我创建了自己的SimpleXMLRPCDispatcher副本,它继承了它的所有内容,并覆盖了2个方法: class MySimpleXMLRPCDispatcher (SimpleXMLRPCDispatcher) : def _marshaled_dispatch(self, data, dispatch_method = None, r
谢谢。好的,我可以。。。有一些妙招 首先,我创建了自己的SimpleXMLRPCDispatcher副本,它继承了它的所有内容,并覆盖了2个方法:
class MySimpleXMLRPCDispatcher (SimpleXMLRPCDispatcher) :
def _marshaled_dispatch(self, data, dispatch_method = None, request = None):
# copy and paste from /usr/lib/python2.6/SimpleXMLRPCServer.py except
response = self._dispatch(method, params)
# which becomes
response = self._dispatch(method, params, request)
def _dispatch(self, method, params, request = None):
# copy and paste from /usr/lib/python2.6/SimpleXMLRPCServer.py except
return func(*params)
# which becomes
return func(request, *params)
那么在我的代码中,要做的就是:
# ...
if len(request.POST):
response = HttpResponse(mimetype="application/xml")
response.write(dispatcher._marshaled_dispatch(request.raw_post_data, request = request))
# ...
def post_log(request, message = "", tags = []):
""" Code called via RPC. Want to know here the remote IP (or hostname). """
ip = request.META["REMOTE_ADDR"]
hostname = socket.gethostbyaddr(ip)[0]
就这样。
我知道它不是很干净。。。欢迎对更清洁的解决方案提出任何建议
# ...
if len(request.POST):
response = HttpResponse(mimetype="application/xml")
response.write(dispatcher._marshaled_dispatch(request.raw_post_data, request = request))
# ...
def post_log(request, message = "", tags = []):
""" Code called via RPC. Want to know here the remote IP (or hostname). """
ip = request.META["REMOTE_ADDR"]
hostname = socket.gethostbyaddr(ip)[0]