Python socket 解析

来源:互联网 发布:p2p理财记账软件 编辑:程序博客网 时间:2024/05/29 16:27

exception socket .error

This exception is raised for socket-related error .The accompanying value is either a string telling what went wrong or a pair(errno,string) representing an error returned by a system callChanged in version 2.6:socket.error is now a child class of IOError.

exception socket.herror

This exception is raised for address-related errors.i.e for functions that use h_errno in the C API, including gethostbyname_ex() and gethostbyaddr().The accompanying value is a pair (h_errno.string)

exception socket.gaierror

This exception is raised for address-related errors. for getaddrinfo() and getnameinfo().The accompanying value is a pair (error,string)

exception socket.timeout

This exception is raised when a timeout occurs on a socket which has had timeouts enable via a prior call to settimeout().The accompanying value is a string whose value is currently always "timed out"

socket.AF_UNIX/socket.AF_INET/socket.AF_INET

 These constants(内容) represent(描绘,表现象征) the address(and protocol) families,used for the first argument to socket. IF the AF_UNIX constant is not defined then this protocol(协议,协定) is unsupported.

socket.SOCK_STREAM/socket.SOCK_DGRAM/socket.SOCK_RAM

socket.SOCK_RDM/socket.SOCK_SEQPACKET

These constants represent the socket types,used for the second argument to socket()

socke.has_ipv6

This constant contains a boolean value which indicates if IPv6 is supported on this platform
import socketprint socket.has_ipv6true

socket.create_connection(address[,timeout[,source_address]])

Connect to a TCP sevice listening on the internet address(a 2 -tuple(host,port)),and return the socket object.This is a higher-level function than socket.connect():if host is a non-numeric hostname,it will try to resolve it for both AF_INET and AF_INET6.

socket.getaddrinfo(host,port[,family[,socketype[,proto[,flags]]]])

Translate the host/port argument into a sequence of 5- tuple  that contain all necessary argument for creating a socket connnected to that service.

socket.getfqdn([name])

Return a fully qualified domain name for name.If name is omitted or empty,it is interpreted as the local host

socket.gethostbyname(hostname)

Translate a host name to IPv4 address format.The IPv4 address is returned as a string ,such as '100.50.200.5'

socket.gethostbyname_ex(hostname)

Translate a host name to IPv4 address format,extended interface.Return a triple(hostname.aliaslist,ipaddrlist)

socket.gethostname()

Retrun a string containing the hostname of the machine where the Python interpreter is currently executing.

socket.gethostbyaddr(ip_address)

Return a triple(hostname ,aliaslist,ipaddrlist)

socket.nameinfo(sockaddr,flags)

Don't Understand!

socket.getprotobyname(protocolname)

Translate an Internet protocol name to a constant suibable for passing as the third argument to the socket()

socket.getservbyname(servicename[,protocolname])

Translate an Internet servie name and protocol name to a prot nubmer for that service

socket.getservbyport(port[,protocolname])

Translate an Internet prot number and protocol name to a service name for that service

socket.socket([family[,type[,proto]]])

Create a new socket using the given address family,socket type and protocol number.

socket.getdefaulttimeout()

Return the default timeout in seconds(float) for new socket objects .A value of None indicates that new socket objects have no timeout. when the socket module is first imported ,the default is None 

socket.setdefaulttimeout(timeout)

Set the default timeout in seconds(float) for new socket objects.A value of None indicates that new socket objects have no timeout.When the socket module is first imported,the default is None.

socket Objects

Socket objects have  the following methods. Except for makefile() these corresond to Unix system call applicable to sockets. 

socket.accept()

Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair(conn,address) where conn is a new socket object usable to send and receive data on the connection,and address is the address bound to the socket on the other end of the connection.

socket.bind(address)

Bind the socket to address. The socket must not already be bound(The format of address denpends on the address family)

scoket.close()

Close the socket. All future  operations on the socket object will fail.The remote end will receive no more date (after queued data is flushed). Sockets are automatically closed when they are grabage-collected(垃圾回收)

socket.connect(address)

Connect to a remote socket at address.(The format of address depends on the address family -see above)

socket.connect_ex(address)

Like connect(address),but return a error indicator instead of raising an  exception for errors returned by the C-level connect() call

socket.fileno()

Return the socket's file  descriptor (a small interger).This is usefull with select.select()Underwindows the small integer returned by this method cannot be used where a file descriptor can be use(such as os.fdopen()).Unix does not have this limitation.

socket.getpeername()

Return the remote address to which the socket is connected .This is useful to find out the port number of a  remote IPv4/v6 socket(The format of the address returned depends on the address family)

soket.getsockname()

Return the socket's own address.This is useful to find out the prot number of an IPv4/Ipv6 socket,for instance.(The format of the address returned denpends on the address family)

socket.getsockopt(level)

0 0