Examples
Here are some real-world examples of how to integrate security.dev into your applications.
User Intelligence Examples
Section titled “User Intelligence Examples”Check if an email is disposable
Section titled “Check if an email is disposable”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.")
IP Intelligence Examples
Section titled “IP Intelligence Examples”Basic IP Geolocation
Section titled “Basic IP Geolocation”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}")
IP-Based Access Control in Django
Section titled “IP-Based Access Control in Django”# 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) )