博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】python中getaddrinfo详解
阅读量:6327 次
发布时间:2019-06-22

本文共 2393 字,大约阅读时间需要 7 分钟。

1. getaddrinfo简介 getaddrinfo可解析得到IPv6地址,而gethostbyname仅能得到IPv4地址。getaddrinfo在Python的socket包中,以下为python官网对getaddrinfo的解释: 转自:
socket.
getaddrinfo
(
host,
port
[,
family
[,
socktype
[,
proto
[,
flags
]
]
]
]
)

Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. host is a domain name, a string representation of an IPv4/v6 address or None. port is a string service name such as 'http', a numeric port number or None. By passing None as the value of host and port, you can pass NULL to the underlying C API.

The family, socktype and proto arguments can be optionally specified in order to narrow the list of addresses returned. By default, their value is 0, meaning that the full range of results is selected. The flags argument can be one or several of the AI_* constants, and will influence how results are computed and returned. Its default value is 0. For example, AI_NUMERICHOST will disable domain name resolution and will raise an error if host is a domain name.

The function returns a list of 5-tuples with the following structure:

(family, socktype, proto, canonname, sockaddr)

In these tuples, family, socktype, proto are all integers and are meant to be passed to the function. canonname will be a string representing the canonical name of the host if AI_CANONNAME is part of the flags argument; else canonname will be empty. sockaddr is a tuple describing a socket address, whose format depends on the returned family (a (address, port) 2-tuple for , a (address, port, flow info, scope id) 4-tuple for ), and is meant to be passed to the method.

The following example fetches address information for a hypothetical TCP connection to www.python.org on port 80 (results may differ on your system if IPv6 isn’t enabled):

>>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)[(2, 1, 6, '', ('82.94.164.162', 80)),(10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0)

 2. socekt.getaddrinfo的返回值介绍

  • family: 表示socket使用的协议簇。常用的协议簇包括AF_UNIX(本机通信)/AF_INET(TCP/IP协议簇中的IPv4协议)/AF_INET6(TCP/IP协议簇中的IPv4协议)。在python的socket包中,用1表示AF_UNIX,2表示AF_INET,10表示AF_INET6。
  • sockettype:表示socket的类型。常见的socket类型包括SOCK_STREAM(TCP流)/SOCK_DGRAM(UDP数据报)/SOCK_RAW(原始套接字)。其中,SOCK_STREAM=1,SOCK_DGRAM=2,SOCK_RAW=3
  • proto:顾名思义,就是指定协议。套接口所用的协议。如调用者不想指定,可用0。常用的协议有,IPPROTO_TCP(=6)和IPPTOTO_UDP(=17),它们分别对应TCP传输协议、UDP传输协议。

 

 

 

 

 

转载地址:http://qzwoa.baihongyu.com/

你可能感兴趣的文章
自学java时的笔记(一)
查看>>
Qt之文本编辑器(二)
查看>>
python编译时检查语法错误
查看>>
考题纠错2
查看>>
SQL——索引
查看>>
Python新手快速入门教程-基础语法
查看>>
JVM性能调优入门
查看>>
关于raid的基本原理、软raid的实现演示
查看>>
科技企业的幕后推手,人工智能究竟有何魔力
查看>>
详解Oracle临时表的几种用法及意义
查看>>
HTML(七)------ 表格
查看>>
如何成为一个设计师和程序员混合型人才
查看>>
unable to load selinux policy. machine is in enforcing
查看>>
2015年10月23日作业
查看>>
MySQL5.7 加强了root用户登录安全性
查看>>
CentOS 6.3_Nagios安装配置与登录
查看>>
加强型的记录集权限(数据集权限、约束表达式设置功能)实现方法界面参考...
查看>>
Linux 内存机制
查看>>
linux下定时任务
查看>>
SharePoint 2013 部署 Part 1
查看>>