0

in do.py

servport = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servport.bind( (socket.gethostname(), 0) )

wrapper = 'doing.py'
pid = os.fork()
if not pid:
       argv = [sys.executable, wrapper, '%s:%d' % servport.getsockname()]
       os.execv(argv[0], argv)
try:
    print "do.py: someone connect to me"
    servport.listen(1)
    (send_sock, got_addr) = servport.accept()
    print "do.py: connected from %s" % str(got_addr)

in doing.py

print "doing.py: got %s" % str(sys.argv)
(host, port) = sys.argv[1].split(':')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect( (host,port) )

I see that '%s:%d' % socket.getsockname() gives out 127.0.1.1:36802

but my public ip address is something that starts with 98...* if i do ifconfig on my box (ubuntu), i have wlan0 of 192.168..

I read and and understood that there are different classes of ip address and 127...* is basically saying that the packet is destined for another app running on the same machine.

How do I code so that '%s:%d' % socket.getsockname() gives out my 98...*?


from suggestions, if i change to servport.bind( ('0.0.0.0', 0) ) in do.py

i get
do.py: someone connect to me
doing.py: got ['doing.py', 0.0.0.0:41107']
do.py: connected from ('127.0.0.1', 43871)

is this mean my packets were actually sent outside of my network and came back? <-----

ealeon
  • 12,074
  • 24
  • 92
  • 173

1 Answers1

0

Referencing this question: Finding local IP addresses using Python's stdlib

Use socket.getfqdn to resolve your qualified host name.

Then use socket.gethostbyname to turn it in to resolvable ip address

Just like this

>>> from socket import gethostbyname, getfqdn

>>> gethostbyname(getfqdn())

'0.0.0.0'

Community
  • 1
  • 1
WebPal
  • 818
  • 8
  • 16