How do I determine the client IP type in Python - IPv4 or IPv6

CK CK 3788 
Created at
Updated at  
760 0 0

We can determine IPv6 and IPv4 based on its separator character. IPv4 uses period(.) and IPv6 uses colon(:).

Below code shows how to determine the client protocol

import socket
def get_client_protocol():
    # Get the client's IP address
    client_ip = socket.getaddrinfo(socket.gethostname(), None)[-1][-1][0]
    # Check if it's an IPv6 address
    if ':' in client_ip:
        return "IPv6"
    # Check if it's an IPv4 address
    elif '.' in client_ip:
        return "IPv4"
    else:
        return "Unknown IP address type"
# Test the function
print("Client is using", get_client_protocol())


 In this Python code:

  1. We import the socket module.
  2. We use socket.getaddrinfo() to get the IP address associated with the hostname of the local machine. We then extract the IP address from the result.
  3. We check if the IP address contains a colon (:), which indicates an IPv6 address, or a dot (.), which indicates an IPv4 address. If neither is found, it's considered an unknown IP address type.

You can run this code to determine whether the client is using IPv4, IPv6, or an unknown IP address type.

Tags IPv4 IPv6 getaddrinfo gethostname socket Facebook X
Comments 0
Similar posts
  1. How do I determine the client IP type (IPv4/IPv6) in PHP
    829
  2. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,067
  3. Enable/Disable IPv4 on Windows
    1,285
  4. Enable/Disable IPv6 on Windows
    1,730
  5. Public DNS (Domain Name Service) based on IPv4, IPv6 widely used
    1,240
  1. Creating mp3 audio file based on the text input with Python
    463
  2. Implementing Text To Speech / Audio with Python3 and pyttsx3
    354
  3. Building Initial Virtual Environment at Python - The first step you should do for Python Programming
    559
  4. Python Simplified Assignment Operators
    533
  5. Trackback module used for Python Threading debugging
    840
  6. Python Flask with simple HTML files to server web pages along with API
    926
  7. Python Flask web server tutorial from the scratch
    618
  8. Simple Web Server based in Python
    368
  9. Thread Implementation in Python
    507
  10. A shell script that activates a Python virtual environment and then runs a specific Python script
    741
  11. Learning about Python Multiprocessing Technique
    590
  12. Simple Example to get the total number of records in BigQuery with Python
    665
  13. Using BigQuery in Python
    407
  14. Python Web Programming based on Django from the Scratch
    366
  15. Flink with Python: Core Concepts, Kafka Integration, and Architectural Trade-offs
    184
  16. Predicting Buyer Preferences with PyTorch: A Deep Learning Approach
    1,230
  17. PyTorch example to Forcast Stock Price based on 10 days Dataset
    651
  18. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,067
  19. Dynamic DNS Made Easy: Building a Python-Based Solution
    793
  20. Milvus & FAISS: Decoding Their ML Synergy
    276
  21. Save clustering result (aka dictionary) in JSON and Reuse loaded result
    376
  22. Forecasting with Linear Regression and KNN Regression in Python
    920
  23. Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction
    1,430
  24. Uvicorn, Gunicorn: Proving Your Production Prowess
    395
  25. Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python
    1,154
  26. Harnessing the Power of Random Forest Algorithm in Python
    657
  27. Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python
    630
  28. Mastering Random Forest Regression: A Comprehensive Guide with Python Examples
    1,093
  29. Simple Python Clustering Example based on the high school students' score
    339
  30. Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network
    799
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,704
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    197
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    508
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    575
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    252
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    326
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252