How can I find my server ip address asynchronously with Twisted ?
I am running an ubuntu and a centos and the results are always the same, the ip returned from the methods exposed belows are always: 127.0.1.1
instead of my real private ip address.
EDIT: this isn't a duplicate of this question as proposed, my last try is inspired from this answer, what I want is a way to achieve this in asynchrounous way.
Trying to retrieve the ip with a tcp server
from twisted.internet import protocol, endpoints, reactor
class FindIpClient(protocol.Protocol):
def connectionMade(self):
print self.transport.getPeer() # prints 127.0.1.1
self.transport.loseConnection()
def main():
f = protocol.ClientFactory()
f.protocol = FindIpClient
ep = endpoints.clientFromString(reactor, 'tcp:127.0.0.1:1234')
ep.connect(f)
reactor.run()
main()
Using reactor.resolve
import socket
from twisted.internet import reactor
def gotIP(ip):
print(ip) # prints 127.0.1.1
reactor.stop()
reactor.resolve(socket.getfqdn()).addCallback(gotIP)
reactor.run()
This works, but I am not sure about its asynchronous-ity
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 0))
s.setblocking(False)
local_ip_address = s.getsockname()[0]
print(local_ip_address) # prints 10.0.2.40
How can I get my private ip address asynchrounously ?
Here is my /etc/hosts
if it can help:
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
127.0.1.1 mymachine mymachine
I don't know why I have 127.0.1.1 mymachine mymachine
in my hosts btw :/