Lets Build - A who is service with flask
Tags: python, html, tutorial, webdev
Published: 2022-05-23 20:00:51

So let's crack out a really nice, simple, and fast Flask website that's super useful to use. In this tutorial, we are going to build a Whois service and get some information back on a domain. 

> Whois service allows you to find all the information about domain name registration, for example, the registration date and age of the domain name, or find contact details of the person or organization owning the domain name of your interest.

So let's get started! Ok, so I'm going to assume that you have Python installed and that you know how to create a virtual environment. So go ahead and do that now and get your terminal open. First things first let's get some packages installed, create a file called app.py and follow them along.

```
pip3 install python-whois flask
```

That's all the packages we are going to need, so let's create a flask app and create two endpoints and return some templates with our HTML.

```python
from flask import Flask, render_template, jsonify, request
import whois
import datetime

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/search', methods=['POST'])
def search():
    url = request.form['url']
    w = whois.whois(url)
    w.expiration_date  # dates converted to datetime object
    datetime.datetime(2013, 6, 26, 0, 0)
    w.text
    return render_template('search.html', url=url, record=w)

if __name__ == '__main__':
    app.run(debug=True, port=8000, host='127.0.0.1')
```

So let's breakdown what's going on. 

We import all our packages here
```python
from flask import Flask, render_template, jsonify, request
import whois
import datetime
```

Then we create a flask instance 
```python
app = Flask(__name__)
```

Here we create a homepage route and return a template Index.html
```python
@app.route('/')
def index():
    return render_template('index.html')
```

Ok, this is the important one, this is the one where we get our data back from the whois package. So first we need to make sure that we can accept a form input, so we add a methods array to our app route. We then define our function and the first thing we do within there is create a variable and give it our form input.

We then call the whois package and pass our new variable to it. We then also pass a date to it and use the DateTime package to do so and on the next bit we get the response back so we can use that to display the URL information.

And lastly, we return a template and pass to that template the URL and URL record.

```python
@app.route('/search', methods=['POST'])
def search():
    url = request.form['url']
    w = whois.whois(url)
    w.expiration_date  # dates converted to datetime object
    datetime.datetime(2013, 6, 26, 0, 0)
    w.text
    return render_template('search.html', url=url, record=w)
```

Finally we have our dunder method and build the flask app
```python
if __name__ == '__main__':
    app.run(debug=True, port=8000, host='127.0.0.1')
```

That's all the Python we need to worry about - The next bit is the front end, so in the following files, we use bootstrap 4 so if you go and download it, create a file called static and place it all in there. 

https://getbootstrap.com/docs/4.0/getting-started/introduction/

Ok so now we are going to create another folder called templates and in that folder, we will have two HTML files called index and search

index.html
```html
<!DOCTYPE html>
<html>

<head>
    <title>Who is Flask</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link
        href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;600&family=Poppins:wght@500&family=Roboto:ital,wght@0,300;0,400;1,100;1,400&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/site.css">
</head>

<body>
    <div class="container">
        <div class="row h-100">
            <div class="col-sm-12 my-auto text-center">
                <h1>Who is - Flask</h1>
                <div class="row">
                    <div class="col-3"></div>
                    <div class="col-6">
                        <form action="/search" method="POST">
                            <div class="form-group">
                                <label for="url">Web url</label>
                                <input type="text" class="form-control" id="url"
                                    name="url">
                                <small id="emailHelp" class="form-text text-muted">Web address should be as follows: webscraping.com</small>
                            </div>
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </form>
   
                 </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
```

search.html
```html
<!DOCTYPE html>
<html>

<head>
    <title>Who is Flask</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link
        href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;600&family=Poppins:wght@500&family=Roboto:ital,wght@0,300;0,400;1,100;1,400&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/site.css">
</head>

<body>
    <div class="container">
        <div class="row h-100">
            <div class="col-sm-12 my-auto text-center">
                <h2 class="mb-2">{{ url }}</h2>
                <br>
                <div class="row">
                    <div class="col-3"></div>
                    <div class="col-6">
                        <ul class="list-group">
                            <li class="list-group-item">Registrar: {{record.registrar}}</li>
                            <li class="list-group-item">Whois Server: {{record.whois_server}}</li>
                            <li class="list-group-item">Name Server 1: {{record.name_servers[0]}}</li>
                            <li class="list-group-item">Name Server 2: {{record.name_servers[1]}}</li>
                            <li class="list-group-item">Email: {{record.emails[0]}}</li>
                            <li class="list-group-item">Name: {{record.name}}</li>
                            <li class="list-group-item">Address: {{record.address}}</li>
                            <li class="list-group-item">City: {{record.city}}</li>
                          </ul>  
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
```

Ok, that's all of our HTML and on the search.html page, you will see code within mustache tags. These are jinga tags that allow us to layout the data we sent through in the search function in app.py. 

So now we have all that sorted we can head to our terminal and use the cool which is service! Let's type the following 

```
python app.py
```

And Hazzah! There you have it! A working whois service that allows you to add any domain and get some information back. 

If you want to clone the code you can grab it on my GitHub

https://github.com/GrahamMorbyDev/whois-flask


[![Whois Python](http://img.youtube.com/vi/jYK7tkQIiss/0.jpg)](https://youtu.be/jYK7tkQIiss)