Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Lets Build - A who is service with flask
Graham Morby
Graham Morby

Posted on

     

Lets Build - A who is service with flask

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
Enter fullscreen modeExit fullscreen mode

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.

fromflaskimportFlask,render_template,jsonify,requestimportwhoisimportdatetimeapp=Flask(__name__)@app.route('/')defindex():returnrender_template('index.html')@app.route('/search',methods=['POST'])defsearch():url=request.form['url']w=whois.whois(url)w.expiration_date# dates converted to datetime objectdatetime.datetime(2013,6,26,0,0)w.textreturnrender_template('search.html',url=url,record=w)if__name__=='__main__':app.run(debug=True,port=8000,host='127.0.0.1')
Enter fullscreen modeExit fullscreen mode

So let's breakdown what's going on.

We import all our packages here

fromflaskimportFlask,render_template,jsonify,requestimportwhoisimportdatetime
Enter fullscreen modeExit fullscreen mode

Then we create a flask instance

app=Flask(__name__)
Enter fullscreen modeExit fullscreen mode

Here we create a homepage route and return a template Index.html

@app.route('/')defindex():returnrender_template('index.html')
Enter fullscreen modeExit fullscreen mode

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.

@app.route('/search',methods=['POST'])defsearch():url=request.form['url']w=whois.whois(url)w.expiration_date# dates converted to datetime objectdatetime.datetime(2013,6,26,0,0)w.textreturnrender_template('search.html',url=url,record=w)
Enter fullscreen modeExit fullscreen mode

Finally we have our dunder method and build the flask app

if__name__=='__main__':app.run(debug=True,port=8000,host='127.0.0.1')
Enter fullscreen modeExit fullscreen mode

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

<!DOCTYPE html><html><head><title>Who is Flask</title><linkrel="preconnect"href="https://fonts.googleapis.com"><linkrel="preconnect"href="https://fonts.gstatic.com"crossorigin><linkhref="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"><linkrel="stylesheet"href="../static/bootstrap.min.css"><linkrel="stylesheet"href="../static/site.css"></head><body><divclass="container"><divclass="row h-100"><divclass="col-sm-12 my-auto text-center"><h1>Who is - Flask</h1><divclass="row"><divclass="col-3"></div><divclass="col-6"><formaction="/search"method="POST"><divclass="form-group"><labelfor="url">Web url</label><inputtype="text"class="form-control"id="url"name="url"><smallid="emailHelp"class="form-text text-muted">Web address should be as follows: webscraping.com</small></div><buttontype="submit"class="btn btn-primary">Submit</button></form></div></div></div></div></div></body></html>
Enter fullscreen modeExit fullscreen mode

search.html

<!DOCTYPE html><html><head><title>Who is Flask</title><linkrel="preconnect"href="https://fonts.googleapis.com"><linkrel="preconnect"href="https://fonts.gstatic.com"crossorigin><linkhref="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"><linkrel="stylesheet"href="../static/bootstrap.min.css"><linkrel="stylesheet"href="../static/site.css"></head><body><divclass="container"><divclass="row h-100"><divclass="col-sm-12 my-auto text-center"><h2class="mb-2">{{ url }}</h2><br><divclass="row"><divclass="col-3"></div><divclass="col-6"><ulclass="list-group"><liclass="list-group-item">Registrar: {{record.registrar}}</li><liclass="list-group-item">Whois Server: {{record.whois_server}}</li><liclass="list-group-item">Name Server 1: {{record.name_servers[0]}}</li><liclass="list-group-item">Name Server 2: {{record.name_servers[1]}}</li><liclass="list-group-item">Email: {{record.emails[0]}}</li><liclass="list-group-item">Name: {{record.name}}</li><liclass="list-group-item">Address: {{record.address}}</li><liclass="list-group-item">City: {{record.city}}</li></ul></div></div></div></div></div></body></html>
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hey guys, 15-year developer! I code with Vue.js and Python, I love to learn but also love to teach! So I try and write informative tutorials and posts. I am new to blogging and would love any feedback
  • Location
    Portsmouth UK
  • Work
    Senior Developer Leighton
  • Joined

More fromGraham Morby

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp