[Tutor] Python Bind DNS Zone config

boB Stepp robertvstepp at gmail.com
Wed May 3 08:24:01 EDT 2017


Hello Nathan!

On Wed, May 3, 2017 at 4:40 AM, Nathan D'Elboux
<nathan.delboux at gmail.com> wrote:

>
> What i am trying to do is have the below syntax somehow stored into a block
> of strings exactly in this syntax. I dont know what data type i should be
> trying to store this as, a string obviously but how would i preserve the
> syntax so it outputs in the identical way? The
>
> zone "." {
> 6     type master;
> 7     //type hint;
> 8     file "/etc/bind/db.root.honeypot";
> 9
>
>
>
>
> };

Do you know about triple quotes in Python?  They can be used when you
wish to have a multi-line string.  You can use either single quotes:

py3: my_string = '''Line one...
... Line two...
... Line three!'''
py3: print(my_string)
Line one...
Line two...
Line three!

Or you could use three double quotes:

py3: my_string = """Line one...
... Line two...
... Line three!"""
py3: print(my_string)
Line one...
Line two...
Line three!

> In between the "." I will be asking a user for input to insert a domain in
> plain ASCII and place it in that location of the zone file config and print
> it to STD OUT on the terminal. Perhaps later another version of this i will
> include writing directly to a file but for now i want to ensure the
> inserting of string into specific position works and just print it out.
>
> So im thinking of something along the lines of
>
> User_input_Zone = str(input("Enter the domain you wish to block: ")

Note:  The str() you use with input() is unnecessary.  input() always
returns a string; no need to convert it to a string.  On the other
hand if the input was an integer or a float, then you would need to
use int() or float(), respectively.

> Def zone_blacklist
> New_zone = "zone "{}" {, user_input_zone
> 6                  type master;
> 7                  //type hint;
> 8                  file "/etc/bind/db.root.honeypot";
> 9 };"
> Print(new_zone)
>
> What i was thinking of doing is having a place holder of {} in place of
> where i want the domain to be inserted into but i dont know how to
> structure the syntax of a zone file in a function within

Have you looked into string formatting?  See:

https://docs.python.org/3/tutorial/inputoutput.html

in the Python Tutorial.  What you appear to be interested in falls a
bit farther down the page.  And you can always google Python 3 string
formatting.

HTH!

-- 
boB


More information about the Tutor mailing list