4

I am using windows 7 and python 2.7 I want to map 172.16.45.84 IP address to myapp.nobies.in without mapping in hosts file.

I have the required certificate to this hostname. I don't want to map in hosts file as it requires administrative privileges.

So, how to create a DNS python server for it, which can be shipped with my app.

imp
  • 1,575
  • 1
  • 24
  • 38
  • This is what public DNS is for. – Joe May 10 '14 at 00:46
  • @Joe the op is asking for a dns solution for which a single hostname can be overridden without local admin rights. public dns will not achieve this. – nettux May 12 '14 at 09:47
  • Is there a reason the DNS server itself has to be in Python? You might be able to find what you're looking for using a pre-built DNS server, and then write your apps that use it in Python. – Brōtsyorfuzthrāx Nov 05 '15 at 00:11

1 Answers1

5

see this post How can I do DNS lookups in Python, including referring to /etc/hosts? for how to do dns lookups in python.

You could do something like:

import socket
name = raw_input("hostname:")
if name == "mpapp.nobies.in":
    print "172.16.45.84"
else:
    print socket.gethostbyname(name)

This will perform normal DNS lookups unless you lookup "myapp.nobies.in" which will return 172.16.45.84

Note: this is not a functioning DNS server application. It is however a (very basic) nslookup-like command alternative. To make this an actual server you need to listen for DNS packets on port 53 (which will require admin rights as it's a privileged port. I guess you could use a higher one but you'd have to configure that on your DNS client too). Investigate socket server programming in python. Good reading here from the python docs:

https://docs.python.org/2/howto/sockets.html

and here:

https://docs.python.org/2/library/socket.html

I'd also suggest looking up dnslib and/or dnspython for parsing DNS packets

EDIT:

try this code as to get you going: (start in a command prompt and minimize)

#!/usr/bin/python

import socket

def resolve(name):
    if name == "mpapp.nobies.in":
        return "172.16.45.84"
    else :
        # you ought to add some basic checking of name here
        return socket.gethostbyname(name)

host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
    client, address = s.accept()
    data = client.recv(size)
    if data:
        bits = data.split(":")
        if bits[0] == 'h':
            client.send(resolve(bits[1]))
    client.close()

and use this as a client: (customize variables and run after you've started the server)

#!/usr/bin/python

import socket

### configure me ###

dns_server_ip = '127.0.0.1'
dns_server_port = 50000
query = 'mpapp.nobies.in' # change this to the hostname you want to lookup

### configure me ###

size = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((dns_server_ip,dns_server_port))
s.send('h:' + query)
data = s.recv(size)
s.close()
print data

Note: this is not really a dns server, it doesn't not understand dns packets it just takes a hostname string prefixed with 'h:' on port 50000 and returns an ip address. I hope this meets your needs.

Usage:

$ START "" .\dns-server.py
$ .\dns-client.py
172.16.45.84
Community
  • 1
  • 1
nettux
  • 4,394
  • 2
  • 20
  • 32
  • Thanks for your answer. I looked into these docs, but did not got any clue for how to make DNS server. – imp May 11 '14 at 18:09
  • @imp do you just want something that will take a hostname and spit out an ip (basically using only A records) or do you need the full power of a proper dns server? – nettux May 12 '14 at 09:52
  • If in hosts file I map 172.16.45.84 myapp.nobies.in then, it is fine working. But writing in host file through code requires administrative privileges. So, without writing in hosts file it should map this IP with hostname. How to do it. – imp May 12 '14 at 11:25
  • @imp editing the hosts file requires admin rights full stop. I believe this answer will do what you need with my latest edits (see line 6 and 7 in the server for the custom entry) The only issue is that this is not really DNS but it'll be fine if you only need to plugin to your own app. IE this will not work in a web browser or with ping etc.. – nettux May 12 '14 at 11:42
  • I tried this, but it didn't work. I am using https://myapp.nobies.in:4440 as URL, WHICH should point to https://172.16.45.84:4440 but 172.16.45.84 myapp.nobies.in should not be map in hosts file. It should automatically map this IP with hostname with python code – imp May 15 '14 at 11:26
  • @imp Where are you putting myapp.nobies.in:4440?? if that's going in to browser it will not work. Attempt to understand my code and you'll see that if it's used correctly it does what you want. (hint: try the bit in the usage section) – nettux May 15 '14 at 13:46
  • https://myapp.nobies.in:4440 is the redirect url of the local https python server. yes it is going in browser. I mapped 172.16.45.84 myapp.nobies.in in hostfile then it is working fine. But requirement is not to write in hosts file. 172.16.45.84 is my local machine IP. – imp May 15 '14 at 17:18
  • @imp If you want to set up a local DNS server without admin rights I'm afraid it can't be done because you need admin rights to open a socket on port 53 (DNS) If by some wizardry you are able to point applications at a different DNS server on a different port then you need to figure out how to encode and decode DNS packets. I'd advise taking a look at the many mini DNS servers written in python on Github – nettux May 19 '14 at 08:23
  • So sir for this particular requirement, apart from DNS server, is there is any other way to do it i.e. map hostname wrt IP – imp May 19 '14 at 13:24
  • @imp You are at the mercy of the name resolution services and priorities configured by the admin on client machines. This is almost always going to be hosts file then configured DNS server (and nothing else). It's the lack of admin rights that make this problem tricky and that's not by mistake... your trying to do stuff that an admin should need to authorize so if you can get it to work it'll be with a *hack* not a solution. The only other thing I can think of which might come close to a solution would be to write a web browser plugin to redirect the user to that IP – nettux May 19 '14 at 13:49