Python Network Programming(2)---地址转换函数

如果使用低层网络函数,有时普通的字符串形式的IP地址(点分十进制)并不是很有用,需要把它们转换成打包后的32位二进制格式。

  • inet_aton()—普通字符串的点分十进制–>32位二进制
  • inet_ntoa()—32位二进制格式–>点分十进制
__author__ = 'liyuan35023'

# !/home/mimiasd/PycharmProjects/workspace python27

import  socket
from binascii import hexlify

ip_addr_list=['127.0.0.1', '192.168.0.1']

def convert_ipv4_address():
    for ip_addr in ip_addr_list:
        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()

代码中还调用了binascii模块的hexlify函数,以十六进制的形式表示二进制数据。

Chapter 1, Sockets, IPv4, and Simple Client/Server Programming, introduces you to Python's core networking library with various small tasks and enables you to create your first clientserver application. Chapter 2, Multiplexing Socket I/O for Better Performance, discusses various useful techniques for scaling your client/server applications with default and third-party libraries. OneKing.Soul Preface [ 2 ] Chapter 3, IPv6, Unix Domain Sockets, and Network Interfaces, focuses more on administering your local machine and looking after your local area network. Chapter 4, Programming with HTTP for the Internet, enables you to create a mini commandline browser with various features such as submitting web forms, handling cookies, managing partial downloads, compressing data, and serving secure content over HTTPS. Chapter 5, Email Protocols, FTP, and CGI Programming, brings you the joy of automating your FTP and e-mail tasks such as manipulating your Gmail account, and reading or sending emails from a script or creating a guest book for your web application. We learn to write email clients with SMTP and POP3. Chapter 6, Programming Across Machine Boundaries, gives you a taste of automating your system administration and deployment tasks over SSH. You can run commands, install packages, or set up new websites remotely from your laptop. Chapter 7, Working with Web Services – XML-RPC, SOAP, and REST, introduces you to various API protocols such as XML-RPC, SOAP, and REST. You can programmatically ask any website or web service for information and interact with them. For example, you can search for products on Amazon or Google. Chapter 8, Network Monitoring and Security, introduces you to various techniques for capturing, storing, analyzing, and manipulating network packets. This encourages you to go further to investigate your network security issues using concise Python scripts. Chapter 9, Network Modeling, introduces you to the world of network simulations and emulations. You learn to simulate networks with NS-3, and emulate networking systems with Mininet and its extensions. Chapter 10, Getting Started with SDN, discusses the enterprise SDN controllers, configuring them to use in Software-Defined Networks. We learn to develop SDN visually with MiniEdit, and configure the networks with OpenDaylight, ONOS, Floodlight, Ryu, and POX controllers. Chapter 11, Authentication, Authorization, and Accounting (AAA), introduces how the networks are secured, and discusses configuring LDAP clients with Python, accounting aspects of the network, and authentication and access of network services. Chapter 12, Open and Proprietary Networking Solutions, discusses in detail, configuring largescale enterprise networking projects, including a few projects from Cisco, Juniper, VMware, and the Linux Foundation.
"Python Network Programming Cookbook, 2nd Edition" ISBN: 1786463997 | 2017 | PDF | 442 pages | 15.41 MB Key Features Solve real-world tasks in the area of network programming, system/networking administration, network monitoring, and more Familiarize yourself with the fundamentals and functionalities of SDN Improve your skills to become the next-gen network engineer by learning the various facets of Python programming Book Description Python is an excellent language to use to write code and have fun by prototyping a pplications quickly. The presence of lots of third-party libraries, also known as batteries, makes it easier and faster to prototype an application or to implement a new algorithm. If you are interested in creating the building blocks for many practical web and networking applications that rely on networking protocols, then this book is a must-have. It employs Python for network programming to solve a variety of problems. This book highlights the major aspects of network programming in Python, starting from writing simple networking clients to developing complex Software-Defined Networking (SDN) systems and programming the Internet. It creates the building blocks for many practical web and networking applications that rely on various networking protocols. It presents the power and beauty of Python to solve numerous real-world tasks in the area of network programming, system and network administration, network monitoring, and web-application development. In this edition, you will also be introduced to network modelling to build your own cloud network. You will learn about the concepts and fundamentals of SDN and then extend your network with Mininet. Next, you'll find recipes on Authentication, Authorization, and Accounting and other alternative vendor-specific SDN approaches and frameworks. You will also learn to configure the Linux Foundation networking ecosystem and automate your networks with Python. By the end of this book, you will be able to analyze your network security vulnerabilities using advanced network packet capture and analysis techniques.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值