ipv4.py

Created by pascal-chauvin

Created on June 07, 2019

1.07 KB

Représentation d’une adresse IPv4.


#!/usr/bin/env python3

def binaire(n):
  """ donne une liste de bits exprimant n """
  b = []
  while n > 1:
    b = [n % 2] + b
    n //= 2
  b = [n % 2] + b

  b = [0] * 8 + b
  b = b[-8:]

  return "".join([str(c) for c in b])



def adresseIP(ip):
  a = [int(x) for x in ip.split('.')]
  b = [binaire(x) for x in a]
  return b



def adresse(ip='192.168.1.1'):
  print(ip)
  a = adresseIP(ip)
#  print(a)
  for b in a:
    print(b)



def classe(ip):
  a = [int(x) for x in ip.split('.')]
  
  o = a[0]
  if 1 <= o and o <= 127:
    return 'classe A'

  if 128 <= o and o <= 191:
    return 'classe B'

  if 192 <= o and o <= 223:
    return 'classe C'

  return 'classe D ou E'



def prive(ip):
  a = [int(x) for x in ip.split('.')]
  o1 = a[0]
  o2 = a[1]
  if o1 == 10:
    return "reseau prive"

  if o1 == 172:
    if 16 <= o2 and o2 <= 31:
      return "reseau prive"

  if o1 == 192 and o2 == 168:
    return "reseau prive"

  return "reseau public"



ip = str(input("adresse IPv4 ? "))
if len(ip) == 0:
  ip = '192.168.1.1'
adresse(ip)
print(classe(ip))
print(prive(ip))