DESCRIPTION This module provides socket operations and some related functions.
On UNIX, it supports IP (Internet Protocol) and UNIX domain sockets. On the other systems, it is only supports IP.
Functions specific for a socket are available as methods of the socket object.
#此模块提供了socket操作和一些相关的功能.
#在Unix上, it supports IP (Internet Protocol) and UNIX domain sockets. #在其他系统上, it only supports IP.
A specific function #插座套接字对象的方法. Functions: #创建一个新的对象 socket ()--Create a new socket object #创建一对新的对象 Socketpair ()--Create a pair of new socket
objects [*] #从一个打开的文件描述符中创建一个socket对象 FROMFD ()--Create a socket object from an open file descriptor [*] #返回当前的主机名
GetHostName ()--Returns the current hostname #I获取主机名的IP地址, must pass a parameter gethostbyname ()--Map a hostname to its IP number #IP地址或者主机的dns信息, you must pass a parameter gethostbyaddr ()--Map an IP number or hostname to DNS info #返回给定服务名和协议的端口号 getservbyname ()--Map a service name and a protocol name to a port number Exp: >>> print socket.getservbyname ("ftp")
>>> print Socket.getservbyname ("http") >>> #协议名称的数量? Getprotobyname ()--Map a protocol name (e.g. ' TCP ') to a number exp: >>> print socket.getprotobyname ("TCP") 6 #从 Network host converts byte order of 16/32 Ntohs (), Ntohl ()-Convert, bit int from network to host byte sequence htons (), htonl ()-Co Nvert bit int from host to network byte order Inet_aton ()-Convert IP addr string (123.45.67.89) to 32-bit PA cked format #32-bit packets are converted to strings (123.45.67.89) Inet_ntoa ()--convert 32-bit packed format IP to string (123.45.67.89) #安 Full socket SLL SSL ()--Secure Socket Layer support (only available if configured) #获取默认的超时值 Socket.getdefaulttimeout ()--Get the default timeout value #设置默认的超时值, the program self-destruct Socket.setdefaulttimeout () after timeout ()--Set the default timeout value #连接到 One address, optional timeout value create_connection ()--connects to a, with a optional timeout [*] not available on all PL
atforms! Special Objects:sockettype--type object forSocket objects Error--exception raised for I/O errors Has_ipv6--Boolean value indicating if IPV6 is supported Integer constants:af_inet, Af_unix-Socket domains (A-argument to socket () call) Sock_stream, Sock_dgra M, Sock_raw--socket types (second argument) Many other constants the May is defined;
The May is used in calls to the setsockopt () and getsockopt () methods. CLASSES __builtin__.object _socketobject _socketobject exceptions. IOError (Exceptions. EnvironmentError) Error Gaierror herror Timeout sockettype = Class _socket Object (__builtin__.object) |
Socket ([family[, type[, Proto]]]-> Socket Object | #打开一个 Specifies the address family for a given type of socket.family, he defaults to the Af_inet,type type is a stream, the default Sock_stream (TCP), or the Data Flow sock_dgram (UDP), the protocol default parameter is 0, Keyword parameters can be received. | Open a socket of the given type. The family argument specifies the | Address family; It defaults to af_inet. The type argument Specifies | Whether this are a stream (Sock_stream, this is the default) | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0, | Specifying the default protocol.
Keyword arguments are accepted.
| #一个socket对象代表一个网络连接 |
A socket object represents one endpoint of a network connection. | #socket对象的方法 (keyword parameter not allowed) | Methods of Socket objects (keyword arguments not allowed): |
Accept ()--Accept a connection, returning the new socket and client address #接受一个连接, returns a newer socket and clients addresses. Exp:print out a two-tuple, typically split (<socket._socketobject object at 0xb7791d84>, (' 127.0.0.1 ', 48336)) | Bind (addr)--bind the socket to a #绑定一个socket到本地地址 | Close ()--Close the socket #关闭socket链接 | Connect (addr)--connect the socket to a remote address #socket连接到远程地址 |
CONNECT_EX (addr)--Connect, return an error code instead of a exception #连接, returns a fault codes instead of exceptions. Exp: >>> s.connect_ex ("www.bianceng.cn",) 0 >>> s.connect_ex (("www.caokbkbkbkbkkbkb.com")) ("S.CONNECT_EX") (" Www.caokbkbkb2222221.com ", 80)) 106 | DUP ()--return a new socket object identical to the current one [*] #在当前返回一个新的socket对象 | Fileno ()--Return underlying file descriptor # Returns a lower-level filename descriptor >>> Print S.fileno () 3 | Getpeername ()--Return remote address [*] #返回远程地址 | GetSockName ()--return home address >>> print s.getpeername () just returned to Baidu (' 61.135.169.125 ', 80) | GetSockOpt (level, optname[, Buflen])--Get socket Options #获取socket选项 | GetTimeout ()--return timeout or None #返回timeout () timeout value | Listen (n)--Start listening for incoming connections #启动监听传入的连接 | Makefile ([mode, [BufSize]])--Return a file object for the socket [*] #返回一个socket的文件对象 | Recv (buflen[, Flags])--Receive Data #接收数据 | Recv_into (buffer[, nbytes[, flags])--receive data (into a buffer) #接收数据, information from the buffer | Recvfrom (buflen[, flags])--Receive data and sender ' s address #输出数据和发送者的地址 | Recvfrom_into (buffer[, nbytes, [, flags]) | --Receive data and sender ' s address (into a buffer) #输出数据和发送者的地址, information from the buffer | Sendall (data[, Flags])--Send all data #发送所有数据. | Send (data[, flags])--send data, may not send all of it #发送数据时, might not be sent all | SendTo (data[, flags], addr)--send data to a given address #将数据发送给一个指定的地址 | Setblocking (0 | 1)--set or clear the blocking I/O flag #设置或清除阻塞IO标志 | SetSockOpt (level, optname, value)--Set socket options #设置socket选项 | settimeout (None | float)--set or clear the timeout #设置或清除超时值 | Shutdown (how)--shut down traffic in one or both directions | #关闭流量在进或出? |
[*] not available on all platforms!
| #定义方法 |
Methods defined here: | |
__init__ (self, family=2, type=1, proto=0, _sock=none) | | Accept (self) |
Accept ()-> (socket object, address info) | | WAIT for a incoming connection. Return a new socket representing the | Connection, and the address of the client. For IP sockets, the Address |
Info is a pair (hostaddr, port).
| | Bind (Self, *args) |
Bind (Address) | | Bind the socket to a. For IP sockets, the address is a | Pair (host, port); The host must refer to the local host. For RAW Packet |
Sockets the address is a tuple (ifname, Proto [, PktType [, Hatype]]) | | Close (self) |
Close () | | Close the socket.
It cannot be used to call.
| | Connect (self, *args) |
Connect (address) | | Connect the socket to a remote address. For IP sockets, the Address |
is a pair (host, port). | #返回一个整型 | CONNECT_EX (self, *args) | CONNECT_EX (address)-> errno exp:import Socket s = socket.socket () Connerror = S.CONNECT_EX (("Www.bianceng.cn) If Connerror = = 0:print "Connect is OK" | | This is like connect (address) but returns an error code (the errno value) |
Instead of raising an exception is an error occurs.
| | DUP (self) |
DUP ()-> Socket Object | |
Return a new socket object connected to the same system resource.
| | Fileno (self, *args) |
Fileno ()-> integer | |
Return the integer file descriptor of the socket.
| | Getpeername (self, *args) |
Getpeername ()-> Address info | | Return to the remote endpoint. For IP sockets, the Address |
Info is a pair (hostaddr, port).
| | GetSockName (self, *args) |
GetSockName ()-> Address info | | Return to the local endpoint. For IP sockets, the Address |
Info is a pair (hostaddr, port).
| | GetSockOpt (self, *args) |
GetSockOpt (level, option[, buffersize])-> value | | Get a socket option.
The Unix Manual for level and option. | If a nonzero buffersize argument is given, the return value is a | string of that length;
Otherwise it is an integer.
| | GetTimeout (self, *args) |
GetTimeout ()-> Timeout | | Returns the timeout in floating seconds associated with socket | Operations. A timeout of None indicates that timeouts on socket |
Operations are disabled.
| | Listen (self, *args) |
Listen (backlog) | | Enable a server to accept connections. The backlog argument must is at | least 1; It specifies the number of unaccepted connection that system |
Would allow before refusing new connections.
| | Makefile (self, mode= ' R ', bufsize=-1) |
Makefile ([mode[, BufSize]])-> File Object | | Return a regular the file object corresponding to the socket. The Mode |
and bufsize arguments are as for the built-in open () function.
| | Sendall (self, *args) |
Sendall (data[, flags]) | | Send a data string to the socket. For the optional Flags | argument, the Unix manual. This calls send () repeatedly | Until all data is sent. If An error occurs, it ' s impossible |
To tell how much data has been sent.
| | Setblocking (self, *args) |
Setblocking (flag) | |
Set the socket to blocking (flag is true) or non-blocking (false). |
Setblocking (True) is equivalent to settimeout (None); |
Setblocking (False) is equivalent to settimeout (0.0).
| | SetSockOpt (self, *args) |
SetSockOpt (level, option, value) | | Set a socket option.
The Unix Manual for level and option. | The value argument can eithEr is an integer or a string.
| | SetTimeout (self, *args) |
settimeout (Timeout) | | Set a timeout on socket operations. ' Timeout ' can be a float, | Giving in seconds, or None. Setting a timeout of None disables |
The timeout feature and is equivalent to setblocking (1). |
Setting a timeout of zero is the same as setblocking (0).
| | Shutdown (self, *args) |
Shutdown (flag) | | Shut down the reading side of the socket (flag = = SHUT_RD), the writing side | The socket (flag = = SHUT_WR), or both ends (flag = = Shut_rdwr).