gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

Chris Angelico rosuav at gmail.com
Sun Sep 1 07:37:34 EDT 2013


On Sun, Sep 1, 2013 at 10:03 AM,  <anntzer.lee at gmail.com> wrote:
> At startup, IPython (qtconsole) calls "socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP addresses that point to the machine. On a Linux server that I manage this call is extremely slow (>20s)... which I have trouble understanding as "ip addr show" seems to give the same information nearly instantaneously. Is there anything I can do to make this faster? Can this be a network configuration issue (I am behind a router)?

Yes, it most definitely CAN be a network config issue. The C function
you want to be calling is getifaddrs(), and I don't think there's a
way to call that from core Python. But a Google search for 'python
getifaddrs' shows up a few third-party modules that might be of use to
you; that'd be a lot quicker and more reliable than trying to look up
your own hostname and depending on the results.

ChrisA



More information about the Python-list mailing list