简体   繁体   English

Python SimpleXMLRPCServer:获取用户IP和简单身份验证

[英]Python SimpleXMLRPCServer: get user IP and simple authentication

I am trying to make a very simple XML RPC Server with Python that provides basic authentication + ability to obtain the connected user's IP. 我正在尝试使用Python制作一个非常简单的XML RPC服务器,该服务器提供基本的身份验证以及获取连接的用户IP的能力。 Let's take the example provided in http://docs.python.org/library/xmlrpclib.html : 让我们来看一下http://docs.python.org/library/xmlrpclib.html中提供的示例:

import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer

def is_even(n):
    return n%2 == 0

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(is_even, "is_even")
server.serve_forever()

So now, the first idea behind this is to make the user supply credentials and process them before allowing him to use the functions. 因此,现在,这背后的第一个想法是让用户提供凭据并对其进行处理,然后再允许其使用功能。 I need very simple authentication, for example just a code. 我需要非常简单的身份验证,例如仅一个代码。 Right now what I'm doing is to force the user to supply this code in the function call and test it with an if-statement. 现在,我正在执行的是强制用户在函数调用中提供此代码,并使用if语句对其进行测试。

The second one is to be able to get the user IP when he calls a function or either store it after he connects to the server. 第二个是当用户调用函数时能够获得用户IP或在连接服务器后将其存储。

Moreover, I already have an Apache Server running and it might be simpler to integrate this into it. 此外,我已经在运行Apache Server,将其集成到其中可能会更简单。

What do you think? 你怎么看?

This is a related question that I found helpful: 这是一个相关的问题,我发现有帮助:

IP address of client in Python SimpleXMLRPCServer? Python SimpleXMLRPCServer中客户端的IP地址?

What worked for me was to grab the client_address in an overridden finish_request method of the server, stash it in the server itself, and then access this in an overridden server _dispatch routine. 对我而言,有效的方法是在服务器的覆盖的finish_request方法中获取client_address,将其存储在服务器本身中,然后在覆盖的服务器_dispatch例程中对其进行访问。 You might be able to access the server itself from within the method, too, but I was just trying to add the IP address as an automatic first argument to all my method calls. 您也许也可以从该方法内部访问服务器本身,但是我只是试图将IP地址作为自动第一个参数添加到我的所有方法调用中。 The reason I used a dict was because I'm also going to add a session token and perhaps other metadata as well. 我使用dict的原因是因为我还将添加会话令牌以及其他元数据。

from xmlrpc.server import DocXMLRPCServer
from socketserver import BaseServer

class NewXMLRPCServer( DocXMLRPCServer):

    def finish_request( self, request, client_address):
        self.client_address = client_address
        BaseServer.finish_request( self, request, client_address)

    def _dispatch( self, method, params):
        metadata = { 'client_address' : self.client_address[ 0] }
        newParams = ( metadata, ) + params
        return DocXMLRPCServer._dispatch( self, method, metadata)

Note this will BREAK introspection functions like system.listMethods() because that isn't expecting the extra argument. 请注意,这会破坏诸如system.listMethods()之类的自省功能,因为这不需要额外的参数。 One idea would be to check the method name for "system." 一种想法是检查“系统”的方法名称。 and just pass the regular params in that case. 并在这种情况下通过常规参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM