如何使用xmlrpclib将client_address公开给所有方法?

时间:2021-02-14 18:10:11

I create little SimpleXMLRPCServer for check ip of client.

我创建了一个简单的SimpleXMLRPCServer来检查客户端的ip。

I try this:

我试试这个:

Server

import xmlrpclib

from SimpleXMLRPCServer import SimpleXMLRPCServer

从SimpleXMLRPCServer导入SimpleXMLRPCServer

server = SimpleXMLRPCServer(("localhost", 8000))

server = SimpleXMLRPCServer((“localhost”,8000))

def MyIp(): return "Your ip is: %s" % server.socket.getpeername()

def MyIp():返回“你的ip是:%s”%server.socket.getpeername()

server.register_function(MyIp)

server.serve_forever()

Client

import xmlrpclib

se = xmlrpclib.Server("http://localhost:8000")

se = xmlrpclib.Server(“http:// localhost:8000”)

print se.MyIp()

Error

xmlrpclib.Fault: :(107, 'Transport endpoint is not connected')">

xmlrpclib.Fault ::(107,'传输端点未连接')“>

How make client_address visible to all functions?

如何使client_address对所有函数可见?

1 个解决方案

#1


If you want for example to pass client_address as the first argument to every function, you could subclass SimpleXMLRPCRequestHandler (pass your subclass as the handler when you instantiate SimpleXMLRPCServer) and override _dispatch (to prepend self.client_address to the params tuple and then delegate the rest to SimpleXMLRPCRequestHandler._dispatch). If this approach is OK and you want to see code, just ask!

例如,如果您希望将client_address作为第一个参数传递给每个函数,则可以将SimpleXMLRPCRequestHandler子类化(在实例化SimpleXMLRPCServer时将子类作为处理程序传递)并覆盖_dispatch(将self.client_address添加到params元组,然后委托其余部分)到SimpleXMLRPCRequestHandler._dispatch)。如果这种方法没问题,你想查看代码,那就问问吧!

I'm not sure how you'd safely use anything but the function arguments to "make client_address visible" -- there's no client_address as a bare name, global or otherwise, there's just the self.client_address of each instance of the request handler class (and hacks such as copying it to a global variables feel really yucky indeed -- and unsafe under threading, etc etc).

我不确定你如何安全地使用除“make client_address可见”的函数参数之外的任何东西 - 没有client_address作为裸名,全局或其他,只有请求处理程序类的每个实例的self.client_address (以及诸如将其复制到全局变量之类的黑客确实感觉真的很令人讨厌 - 并且在线程等下不安全)。

#1


If you want for example to pass client_address as the first argument to every function, you could subclass SimpleXMLRPCRequestHandler (pass your subclass as the handler when you instantiate SimpleXMLRPCServer) and override _dispatch (to prepend self.client_address to the params tuple and then delegate the rest to SimpleXMLRPCRequestHandler._dispatch). If this approach is OK and you want to see code, just ask!

例如,如果您希望将client_address作为第一个参数传递给每个函数,则可以将SimpleXMLRPCRequestHandler子类化(在实例化SimpleXMLRPCServer时将子类作为处理程序传递)并覆盖_dispatch(将self.client_address添加到params元组,然后委托其余部分)到SimpleXMLRPCRequestHandler._dispatch)。如果这种方法没问题,你想查看代码,那就问问吧!

I'm not sure how you'd safely use anything but the function arguments to "make client_address visible" -- there's no client_address as a bare name, global or otherwise, there's just the self.client_address of each instance of the request handler class (and hacks such as copying it to a global variables feel really yucky indeed -- and unsafe under threading, etc etc).

我不确定你如何安全地使用除“make client_address可见”的函数参数之外的任何东西 - 没有client_address作为裸名,全局或其他,只有请求处理程序类的每个实例的self.client_address (以及诸如将其复制到全局变量之类的黑客确实感觉真的很令人讨厌 - 并且在线程等下不安全)。