0

http://code.djangoproject.com/wiki/XML-RPCからインスピレーションを得たコードを持つ:

from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from django.http import HttpResponse

dispatcher = SimpleXMLRPCDispatcher(allow_none=False, encoding=None) # Python 2.5

def rpc_handler(request):
    """
    the actual handler:
    if you setup your urls.py properly, all calls to the xml-rpc service
    should be routed through here.
    If post data is defined, it assumes it's XML-RPC and tries to process as such
    Empty post assumes you're viewing from a browser and tells you about the service.
    """

    if len(request.POST):
        response = HttpResponse(mimetype="application/xml")
        response.write(dispatcher._marshaled_dispatch(request.raw_post_data))
    else:
        pass # Not interesting
    response['Content-length'] = str(len(response.content))
    return response

def post_log(message = "", tags = []):
    """ Code called via RPC. Want to know here the remote IP (or hostname). """
    pass

dispatcher.register_function(post_log, 'post_log')

「post_log」定義内でクライアントのIPアドレスを取得するにはどうすればよいですか?Python SimpleXMLRPCServerでクライアントのIPアドレスを見たことがありますか?しかし、私の場合には適用できません。

ありがとう。

4

1 に答える 1

0

わかりました...いくつかの気の利いたヒントで...

最初に、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]

それでおしまい。私はそれがあまりきれいではないことを知っています...よりきれいな解決策についての提案は大歓迎です!

于 2010-10-05T14:05:20.307 に答える