Ad

Python Dynamically Set DNS Record To IP (in Code Not By By Changing Hosts File)

- 1 answer

Is there a way to point a DNS record to IP by code? Example: Lets say www.mydns.example is pointing to IP 192.0.2.4, which is the firewall address. I want to run a python script from a serverless script (like Amazon's) to reach directly to my "real" server which is on IP 192.0.2.8 (bypassing the firewall)

When I run it from a server I can change the hosts file. but I want to do it by code so I won't have to edit the local hosts file (which not functional with server-less function).

something like this:

url = "www.mydns.example"
response=urllib2.urlopen(url,ip = '192.0.2.8')

Any idea?

Ad

Answer

Issue is solved by doing the following:

the URL should be with the IP address : http://192.0.2.8/index/php and using urllib2.Request, I configured the headers and added the domain under 'host'.

like this:

import urllib2

req = urllib2.Request('http://192.0.2.8/',headers={'host':'www.mydns.example'})
response=urllib2.urlopen(req)
Ad
source: stackoverflow.com
Ad