Reputation: 103
I'm hacking together a simple RPC server in Python for use on internal networks in a laboratory environment. When I create a server with code like:
server = SimpleXMLRPCServer(("", 8000))
the server seems to listen on multiple available interfaces (i.e., both localhost and the computer's public IP address) just like I want it to.
Is there a simple and preferably portable way for my server script to determine which IP addresses it is listening on?
It will generally be run on random Windows machines with dynamic addresses and python 2.6 installed, and it would be helpful if my server could print out some sort of "Listening on address1:port address2:port..." message when it's launched. I've seen other questions related to getting client addresses, but I'm looking for the server address(es).
Upvotes: 3
Views: 589
Reputation: 1418
import socket
print([(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])
I found this page which had several other useful methods: Finding local IP addresses using Python's stdlib
Upvotes: 1