Skip to content
GitHub

Examples

Here are some real-world examples of how to integrate security.dev into your applications.

from securitydev import SecurityClient

client = SecurityClient(key="<your_app_key>")

e = client.user_intel.email("[email protected]")
if e.is_disposable:
    raise ValidationError("Disposable e-mails are not allowed.")
from securitydev import SecurityClient

client = SecurityClient(key="<your_app_key>")

# Get geolocation data for a public DNS resolver
geo = client.ip_intel.geolocate("9.9.9.9")
print(f"IP is located in {geo.city.name}, {geo.country.name}")
# In your Django view or middleware
from securitydev import SecurityClient
from django.http import HttpResponseForbidden

client = SecurityClient(key="<your_app_key>")

def secure_view(request):
    # Get client IP
    ip = request.META.get('REMOTE_ADDR')

    # Check IP reputation
    rep = client.ip_intel.reputation(ip)

    # Block abusers
    if rep.is_abuser:
        return HttpResponseForbidden("Access denied due to security concerns")

    # Block TOR exit nodes
    if rep.is_tor_exit:
        return HttpResponseForbidden("Access via TOR is not allowed")

    # Continue with the regular view logic
    return render(request, 'secure_page.html')

Country-Based Content Personalization in Flask

Section titled “Country-Based Content Personalization in Flask”
# In your Flask route
from flask import Flask, render_template, request
from securitydev import SecurityClient

app = Flask(__name__)
client = SecurityClient(key="<your_app_key>")

@app.route('/')
def homepage():
    # Get client IP
    ip = request.remote_addr

    # Get geolocation data
    geo = client.ip_intel.geolocate(ip)

    # Personalize content based on country
    country_code = geo.country.iso_code

    return render_template(
        'homepage.html',
        country=geo.country.name,
        local_content=get_local_content(country_code)
    )