Results 1 to 6 of 6

Thread: ip address ( python )

  1. #1
    Join Date
    Aug 2008
    Beans
    2

    Question ip address ( python )

    Hello, i have a small question:

    on windows the
    Code:
    socket.gethostbyname(socket.gethostname())
    function would return my ip (eg 79.116.238.19) which i could use for a server ( socket.bind() )

    on ubuntu i get the localhost address, thus rendering the server useless. i tried binding to my ip address directly or using the domain name from no-ip (something@no-ip.org -> ip address) but i get (99, 'Cannot assign requested address') error.

    thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Ann Arbor, MI
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: ip address ( python )

    I'm not sure exactly what you're trying to do here. If you're just trying to get your own machine's IP using python commands, maybe this little tweak (from http://mgltools.scripps.edu/document...p-using-python):
    Code:
    import socket
    socket.gethostbyname(socket.gethostname())
    socket.gethostbyname_ex(socket.gethostname())
    I'm like hacks too, so I would probably try something like
    Code:
    os.system("ifconfig > file.txt")
    and then read the text file, but that's obviously a crappy way to go about writing clean code.

  3. #3
    Join Date
    Feb 2006
    Location
    Ann Arbor, MI
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: ip address ( python )

    Nevermind, the gethostbyname_ex() doesn't work either. I'll keep poking around on the internet, but that hack of mine doesn't seem quite so terrible now...

  4. #4
    Join Date
    Feb 2006
    Location
    Ann Arbor, MI
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: ip address ( python )

    Ok, I think I got it this time (courtesy http://kryptoz.wordpress.com/2008/01...nd_ip_address/):
    Code:
    from socket import socket, SOCK_DGRAM, AF_INET
    s = socket(AF_INET, SOCK_DGRAM)
    s.connect(('google.com', 0))
    s.getsockname()
    I tried it out and it worked for me. Not sure why connecting to an external site (google here) is so important, but I ran across several people saying that it's necessary, so I suppose it is.

  5. #5
    Join Date
    Aug 2008
    Beans
    2

    Re: ip address ( python )

    Hey, thanks for answering. My actual problem was making socket.bind() to work with my ip address instead of localhost. I didn't really knew what was the problem, why socket.bind("",port) would not use my ip address so i asked why my ip retrieving method failed. Well, i tried your method and it worked. I can now see my ip and bind it so i can accept incoming connections.

    Thanks a lot man, i really appreciate your help

  6. #6
    Join Date
    Feb 2006
    Location
    Ann Arbor, MI
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: ip address ( python )

    No problem! I'm glad that solution worked so well for you.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •