Python Sokcet 文档翻译
2016-03-07 08:59
579 查看
Scoket Low level networking interface(网络接口)
This module provides aceess to(接近,通路) the BSD socket interface. It is available on all modern Unix systems ,windows,Mac OS X ,BeOS ,OS/2 ,and probably additional platforms(平台).
Note:
Some behavior may be platform dependent ,since calls are made to the operating system socket APIs.
For an introduction to socket programming (int C), see the following papers :An introductory 4.3BSD interprocess Communication Tutorial, by Stuart Sechrest and An Advanced 4.3BSD interporcess Comu….
The Python interface is a straightforward transliteration(直译) of the Unix system call and library interface for sockets to Python’s object-oriented style : the socket() function returns a socket object whose methods implement(工具:执行:实现)the various socket system calls. Parameter types are somewhat higher-level than in the C interface : as with read() and write () operations on Python files , buffer allocation(分配) on receive operations is automatic , and buffer length is implicit(暗示,含蓄) on send operations.
Socket addresses are represented as follows : A single string is used for the AF_UNIX address family . A pair(host , port ) is used for the AF_INET address family where host is a string representing either a hostname in Internet domain notion like “daring.cwi.nl” or an IPv4 address like ‘100.50.200.5’, and port is an integer. For AF_INET anddresss family ,a four-tuple(host ,port,flowinfo ,scopeid) is used, where flowinfo and scopeid represents sin6_flowinfo and sin6_scope_id member in struct sockaddr_in6 in C . For socket module methods, flowinfo and scopeid can be omitted(忽略,遗漏) just for backword compatibility(兼容,适用).Note , however, omisson of scopeid can cause problems in manipulating scoped IPv6 addressed.Other address families are currently not support .The address format required by a particular socket object is automatically selected base on the address family specified when the socket object was created.
For IPv4 addresses , two special forms are accepted instead of a host address: the empty string represent(描绘,表现,象征,作为) INADD_ANY, and the string “broadcast” represents INADDR_BROADCAST. The behavior is not available for IPv6 for backward compatibility. therefore ,you may want to avoid these if you intend to support IPv6 with your Python programs.
If you use a hostname in the host portion of IPv4/v6 socket address, the program may show a nondeterministic(非确定的) behavior, as Python uses the first address returned from the DNS resolution. The socket address will be resolved differently into an actual IPv4/v6 address ,denpending on the results from DNS resolution and/or the host configuration .For deterministic(决定性的) behavior use a numeric address in host portion
New in version 2.5:i AF_NETLINK sokcets are represented as pairs pid,groups. New in version 2.6: Linux-only support for TIPC is also available using the AF_TIPC address family.TIPC is an open. no-IP based networked protocol designed for use in clustered computer environments .Addresses are represented by a tuple ,and the fields depend on the address type. The general tuple form is (addr_type , v1 ,v2 ,v3 [,scope]),where: 1 addr_type is one of TICP_ADDR_NAMESEQ,TIPC_ADDR_NAME,or TIPC_ADDR_ID 2 scope is one of TIPC_ZONE_SCOPE,TIPC_CLUSTER_SCOPE,and TIPC_NONE_SCOPE. 3 if addr_type is TIPC_ADDR_NAME ....
socket.creat_connetion(address[,timeout[,source_address]])
Connect to a TCP servie listening on the Internet address(a 2-tuple(host,prot)),and return the socket object. This is a higher-level function than socket.connect(): if host is a non-numeric(无数值的) hostnanme, it connect to all possible addresses in turn until a connection succeeds.This makes it easy to write clients that are compatible to both IPv4 and IPv6. Passing the optional timeout parameter will set the timeout on the socket instance before attempting to connect. if no timeout is is supplied , the global default timeout setting returned by getdefaulttimeout() is used. If supplied , source_address must be 2-tuple (host,port ) for the socket to bind to as its source address before connecting. If host or port are '' or 0 respectively the OS default behavior will be used. Changed in version 2.7: source_address was added.
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 , sockettype and proto arguments can be optionally specified(详情) in order to narrow (狭窄)the list of addressed 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_NUMBERICHOST will disable domain name resolution(域名解析) and will raise an error if host is domain name The function returns a list of 5-tuples with the following structure: (family, socktype,proto,cononname,sockaddr) In these tuples, family , socktype,proto are all intergers and are meant to be passed to the socket() function。 canonname will be a string representing the cannonical name of the host if if AI_CANONNAME is part of the flags argument; else canonname will be empty , sockaddr is a tuple describing a socket addresss, whose format depends on the returned family (a (address,port )2-tuple for AF_INET, a(address, port ,flowinfo,scope id)4-tuple for AF_INET6,and is meant to be passed to the socket.connect)method.
socket.getfqdn([name])
return a fully qualified domain name for name. If name is omitted or empty,it is interpreted as the local host. To find fully qualified name,the hostname returned by gethostbyaddr() is checked,followed by aliases for the host,if available .The first name which includes a period is selected .Incase no fully qualified domain name is available,the hostname as returned by gethostname()is returned.
socket.gethostbyname(hostname)
Translate a host name to IPv4 address format.The IPv4 address is returned as a string ,such as '10.50.200,5'.if the host name is an IPv4 address itself it is return unchanged.See gethotbyname_ex()for a more complete interface.gethostbyname() does not support IPv6 name resolution ,and get addrinfo() should be used instead for IPv4/v6 dual stack supprot.
socket .gethostbyname_ex(hostname)
Translate a host name to IPv4 address format, extended interface. Return a triple(hostname,aliaslist,ipaddrlist) where hostname is the primary host name responding to the gvien ip_address,aliaslist is a (possibly empty) list of alternative host names for the same address,and ipaddrlist is a list of IPv4 addresses for the same interface on the same interface on the same host(often but not always a single address).gethostbyname_ex()does not support IPv6 name resolution,and getaddrinfo() should be used instead for Ipv6/v6 dual stack support.
socket.gethostname()
Return a string containing the host name of the machine where the Python interpreter is currently executing if you want to the current machine's IP address, you may want to use gethostbyname(gethostname()). This operation assumes that there si a valid address-to-host mapping for the host, and the assumption does not always hold Not:gethostname() doesn't always return the fully qualified domain name; use getdqfn()(see above)
socket.gethostbyaddr(ip_address)
Return a
相关文章推荐
- java-模拟tomcat服务器
- Linux socket 初步
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例