tags:

views:

59

answers:

3

I have domain on a shared hosting provider. How do I find the direct IP address of my domain using Python?

Is it possible to post to a script on my domain using the IP address and not the website itself?

Thanks.

A: 
  1. I guess the IP should be static so do you really need to look it up more than once?

  2. You need to specify the domain name so that the webserver knows which host configuration to use if you don't have a dedicated IP or your host is the default for that webserver

baloo
How do I specify the domain name if I am using urllib2: urllib2.urlopen('101.21.00.01')
Ali
A: 
import socket
socket.gethostbyname("www.stackoverflow.com")
'69.59.196.211'

will get you the ip address (as a string) of your domain.

However, if it's shared hosting I would think it highly unlikely that you'll be able to access your hosting via the ip - most likely you'll have something like Apache's VirtualHost Directive in place which limits you to only 'seeing' requests to your domain. Requests to the IP address will be served by some default configuration.

Would very much depend on the nature of your hosting.

pycruft
Thanks for the code, but how do I specify which domain name is the request for? Urllib2 will allow me to specify the IP address but does it allow for domain name?
Ali
A: 

A curious request ...

To look up a domain name, do something like this:

import socket
ipaddress = socket.gethostbyname('www.bbc.co.uk')

Regarding posting to the IP address: I don't think it would work in the normal way (like from a browser), because there will probably be many sites held under that address.

But, I guess you could do it in a very manual way, using a programming language (e.g. Python), if you connected a client socket to the site's IP address, but still sent the website's name in the HTTP Host request header.

I don't know if that poses more questions than it answers, and I don't know why you'd want to do either of the above, but there it is.

Good luck!

amir75
Thanks for the reply. How do I post the domain name along with the IP address? urllib2.urlopen('101.21.00.01', domain name)?
Ali
No, you wouldnt be able to do it with urllib. You'd need to use socket.send() to send an entire http post. You'd need to understand what an HTTP POST looks like in its raw form. See http://docs.python.org/library/socket.html and http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
amir75
Having said all that, I'm guessing that you're simply trying to bypass a the need for a DNS lookup, and if possible, I'd sooner just add it as an entry into your computer's 'hosts' file instead. http://en.wikipedia.org/wiki/Hosts_file
amir75