Client IP address in Python SimpleXMLRPCServer?

I have a SimpleXMLRPCServer server (Python).

How can I get the client IP address in the request handler?

This information appears in the journal. However, I am not sure how to access this information from the request handler.

+3
source share
3 answers

As Michael noted, you can get client_addressfrom the request handler. For example, you can override a function __init__that inherits indirectly from BaseRequestHandler.

class RequestHandler(SimpleXMLRPCRequestHandler):
    def __init__(self, request, client_address, server):
        print client_address # do what you need to do with client_address here
        SimpleXMLRPCRequestHandler.__init__(self, request, client_address, server)
+3
source

client_address ( BaseHTTPRequestHandler). BaseHTTPRequestHandler:

(host, port), .

+2

One way to pass the IP address to the request method is to override RequestHandler.decode_request_content.

decode_request_content returns an XML string. Example:

<?xml version='1.0'?>
<methodCall>
    <methodName>get_workunit</methodName>
    <params>
        <param>
            <value><int>1</int></value>
        </param>
        <param>
            <value><string>Windows</string></value>
        </param>
        <param>
            <value><string>32bit</string></value>
        </param>
    </params>
</methodCall>

Just add another parameter.

class HackyRequestHandler(SimpleXMLRPCRequestHandler):
    def __init__(self, req, addr, server):
       self.client_ip, self.client_port = addr
       SimpleXMLRPCRequestHandler.__init__(self, req, addr, server)
    def decode_request_content(self, data):
       data = SimpleXMLRPCRequestHandler.decode_request_content(self, data)
       from xml.dom.minidom import parseString
       doc = parseString(data)
       ps = doc.getElementsByTagName('params')[0]
       pdoc = parseString(
            ''' <param><value>
                <string>%s</string>
                </value></param>''' % (self.client_ip,))
       p = pdoc.firstChild.cloneNode(True)
       ps.insertBefore(p, ps.firstChild)
       return doc.toxml()

and update your method signatures accordingly.

+2
source

Source: https://habr.com/ru/post/1729828/


All Articles