[Tutor] problem with reading dns query

Steven D'Aprano steve at pearwood.info
Sun Jul 3 07:08:16 CEST 2011


preetam shivaram wrote:
> I have got a very simple idea in mind that i want to try out. Say i have a
> browser, chrome for instance, and i want to search for the ip of the domain
> name, say `www.google.com`. I use windows 7 and i have set the dns lookup
> properties to manual and have given the address `127.0.0.1` where my server
> (written in Python) is running. I started my server and i could see the dns
> query like this:


It seems to me that you want to learn how to write your own DNS caching 
server. To do that, you have to understand the DNS protocol. Start with 
the Wikipedia article:

http://en.wikipedia.org/wiki/Domain_Name_System

We probably can't help you here, this is a list for learning Python, not 
DNS. You might have more luck on the main Python list, 
<python-list at python.com> or news group <comp.lang.python> But generally, 
I would expect they'll give you the same advice I am giving: google is 
your friend.

Search for "python DNS server" and you will find information that may be 
useful to you.


More comments below:


>     WAITING FOR CONNECTION.........
> 
>     .........recieved from :  ('127.0.0.1', 59339)
> 
> "'~\\x17\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x03www\\x06google\\x02co\\x02in\\x00\\x00\\x01\\x00\\x01'"
> 
> The `waiting for connection` and the `received from` is from my server. How
> do i get a breakdown form(a human readable form) of this message??

Define "human readable form".

The data you get includes binary bytes, that is, you are getting:

tilde
hex byte 17
hex byte 01
null byte
null byte
hex byte 01

etc.

How would you like the string to be displayed, if not with escaped hex 
codes?

If you just print the string, the binary bytes will probably disappear 
because your terminal doesn't show them. You can convert to human 
readable form with escaped binary bytes using repr() like this:


 >>> print s  # non-printable characters don't print
~wwwgooglecoin
 >>> print repr(s)
'~\x17\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x02co\x02in\x00\x00\x01\x00\x01'


You could also try a hex dump, there are many recipes on the Internet. 
Here's the first one I tried:

http://code.activestate.com/recipes/142812-hex-dumper/

 >>> print(dump(s))
0000   7E 17 01 00 00 01 00 00    ~.......
0008   00 00 00 00 03 77 77 77    .....www
0010   06 67 6F 6F 67 6C 65 02    .google.
0018   63 6F 02 69 6E 00 00 01    co.in...
0020   00 01                      ..



-- 
Steven



More information about the Tutor mailing list