shahjapan
shahjapan

Reputation: 14355

how to get client ip in soap web service method?

I'm using soaplib library for SOAP web services, I want to access client's IP Address when client request has been made in my web service method.

class HelloWorldService(DefinitionBase):
    @soap(String, Integer, _returns=Array(String))
    def say_hello(self, name, times):
        # here I want to Access client's IP Address
        pass

Here is my Server's pseudo code...

server = make_server('localhost',
                     7789, 
                     HelloWorldApplication(Application([HelloWorldService], 'hello')))
print "serving http://localhost:7789/hello?wsdl"
server.serve_forever()

Upvotes: 0

Views: 1886

Answers (2)

d3v11
d3v11

Reputation: 1

Solved:

's/self.connection.getsockname()[0]/self.client_address[0]/' 

Upvotes: 0

J.J.
J.J.

Reputation: 5069

From the make_server I'm assuming you're using BaseHTTPServer or one of the packages that builds on top of it.

In your request handling code, you should be able to find a reference to the client socket. Use that to get getsockname() and retrieve the client IP and TCP source port.

For example, if the socket reference was in the base class as socket, you could:

class HelloWorldService(DefinitionBase):
    @soap(String, Integer, _returns=Array(String))
    def say_hello(self, name, times):
        # here I want to Access client's IP Address
        print "Client IP:", self.socket.getsockname()[0]

My hacked-up example, to retrieve the IP of the listening socket on the server: (this is not the client socket!)

>>> httpd = make_server('', 8000, simple_app)
>>> dir(httpd)
['RequestHandlerClass', '_BaseServer__is_shut_down', '_BaseServer__shutdown_request', '__doc__', '__init__', '__module__', '_handle_request_noblock', 'address_family', 'allow_reuse_address', 'application', 'base_environ', 'close_request', 'fileno', 'finish_request', 'get_app', 'get_request', 'handle_error', 'handle_request', 'handle_timeout', 'process_request', 'request_queue_size', 'serve_forever', 'server_activate', 'server_address', 'server_bind', 'server_close', 'server_name', 'server_port', 'set_app', 'setup_environ', 'shutdown', 'shutdown_request', 'socket', 'socket_type', 'timeout', 'verify_request']
>>> dir(httpd.socket)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_sock', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family', 'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking', 'setsockopt', 'settimeout', 'shutdown', 'type']
>>> httpd.socket.getsockname()
('0.0.0.0', 8000)

Getting the actual client IP is a little more involved example, since you don't get a reference to the client socket object until the request handler callback is called. An example using BaseHTTPServer:

import BaseHTTPServer

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("Client IP: %s" % self.connection.getsockname()[0])

httpd = BaseHTTPServer.HTTPServer(("", 8000), Handler)
httpd.serve_forever()

I don't have soaplib on a system here to give you a more precise example, but the socket object is tucked away in there somewhere.

Upvotes: 1

Related Questions