Converting an IPv4 address to different formats

#!/usr/bin/env python

import socket
from binascii import hexlify

def convert_ipv4_address():
	for ip_addr in [‘127.0.0.1‘,‘192.168.0.1‘]:
		packed_ip_addr = socket.inet_aton(ip_addr)
		unpacked_ip_addr = socket.inet_ntoa(packed_ip_addr)
		print "IP Address: %s => Packed:%s, Unpacked: %s"		%(ip_addr,hexlify(packed_ip_addr),unpacked_ip_addr)

if __name__ == ‘__main__‘:
	convert_ipv4_address()

Description:

The Python socket library has utilities to deal with the various IP address formats.

Here, we will use two of them: inet_aton() and inet_ntoa(). Let us create the

convert_ip4_address() function,where inet_aton() and inet_ntoa() will be used for

the IP address conversion.We will use two sample IP addresses, 127.0.0.1 and 192.168.0.1.

TEST:

IP Address: 127.0.0.1 => Packed:7f000001, Unpacked: 127.0.0.1
IP Address: 192.168.0.1 => Packed:c0a80001, Unpacked: 192.168.0.1
[Finished in 0.1s]

时间: 2024-11-03 01:22:32

Converting an IPv4 address to different formats的相关文章

Get IPv4 Address 2.0

main.bat 1 @echo off 2 color 0a 3 title Get IPv4 Address 2.0 4 5 SETLOCAL ENABLEEXTENSIONS 6 SETLOCAL ENABLEDELAYEDEXPANSION 7 8 set "CS_1=IPv4 Address. . . . . . . . . . . : " 9 10 for /f "tokens=* delims=" %%i in ('ipconfig') do ( 11

在Linux上使用C编程获取IPv4地址及子网掩码

在Linux上(如Ubuntu或CentOS), 获取某个Network Interface比如eth0的IP地址等信息,我们可以使用ifconfig或者ip addr show命令. $ ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:25:64:ba:8d:be inet addr:192.168.1.102 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::225:64ff:f

NAT (Network Address Translation)

NAT Introduction NAT: Provides the translation of private address to public address. ? NAT has many uses, but its primary use is to conserve public IPv4 addresses. It does this by allowing networks to use private IPv4 addresses internally and providi

[LeetCode] Validate IP Address 验证IP地址

In this problem, your job to write a function to check whether a input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging

[转]Peer-to-Peer Communication Across Network Address Translators

Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of Technology baford (at) mit.edu Pyda Srisuresh Caymas Systems, Inc. srisuresh (at) yahoo.com Dan Kegel dank (at) kegel.com J'fais des trous, des petits

Leetcode: Validate IP Address

In this problem, your job to write a function to check whether a input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging

Android获取本地IP地址,Ipv4地址检查,Ipv6地址检查

/** * 获取本地IP地址 * @author YOLANDA * @return */ public static String getLocalIPAddress() { String ipAddress = ""; try { Enumeration<NetworkInterface> netfaces = NetworkInterface.getNetworkInterfaces(); // 遍历所用的网络接口 while (netfaces.hasMoreEle

套接字编程简介: IPV4套接字地址结构/ 通用套接字地址结构/ IPV6套接字地址结构

IPv4套接字地址结构通常也称为"网际套接字地址结构",它以sockaddr_in命名,定义在<netinet/in.h>头文件中. struct in_addr { in_addr_t s_addr;/*32-bit IPv4 address*/ /*network byte ordered*/ }; struct sockaddr_in { uint8_t sin_len;/*length if structure(16)*/ sa_family_t sin_famil

468. Validate IP Address

Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by