views:

73

answers:

1

I basically want to implement something where you can type in any URI ( I probably will only deal with http ) and I want to return the A record of the domain in the URI, I want the server's IP address.

I know there's the ping command which most people use to look an ip address up, but I also know there's 'host' and 'dig' which are more specific.

Are there any native functions I can use that will do this for me? And if so, how lenient is that function in terms of what URI string it accepts and the structure it's in? I want to throw it:

And have basically anything return an IP address. If need be I can take care of the URI parsing ( so it's a consistent format when looked up ) myself, that's an extra plus though.

+4  A: 
py> import urlparse,socket
py> p = urlparse.urlparse("http://stackoverflow.com/questions/1480183")
py> p
('http', 'stackoverflow.com', '/questions/1480183', '', '', '')
py> host=p[1]
py> ai=socket.gethostbyname(host)
py> socket.gethostbyname(host)
'69.59.196.211'
Martin v. Löwis
just what I was looking for, much appreciated.
meder
It freaks me out how much some people know about python. I'm am just a pathetic hacker, yet with python I still manage to accomplish stuff.
twneale
p[1] is not the host in:>>> p = urlparse.urlparse("www.stackoverflow.com")>>> p('', '', 'www.stackoverflow.com', '', '', '')>>> p[1]''
foosion
@foosion: correct. If the input is just a host name, not a URL, the OP would need to special-case it.
Martin v. Löwis