1

I write chat using crossbar.io. We have several nodes of chat. I need write statistics about each of nodes, that's why I need to get host name where specific node is run.

Is it possible get host name from component instance?

I use last version of crossbar/autobahn and python 3.4.

Expect get - 127.0.0.1 if I use local environment.

peterh
  • 11,875
  • 18
  • 85
  • 108
Oleksandr Yarushevskyi
  • 2,789
  • 2
  • 17
  • 24

2 Answers2

2

Just use the socket library:

import socket

hostname = socket.gethostname()
Pierre Barre
  • 2,174
  • 1
  • 11
  • 23
2

In case of your machine has a resolvable hostname try with:

import socket socket.gethostbyname(socket.getfqdn())

Update. This is a more complete solution, should work fine with all OS:

import socket
print [l for l in ([ip for ip in socket.gethostbyname_ex( socket.gethostname())[2] if not ip.startswith('127.')][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close() ) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]

pualien
  • 138
  • 7