DESCRIPTION
This module provides socket operations and some related functions.
On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
On other systems, it only supports IP. Functions specific for a
socket are available as methods of the socket object.
#此模組提供了socket操作和一些相關的功能。
#在Unix上,它支援IP(網際網路協議)和Unix域sockets。
#在其他系統上,它僅支援IP。一個特定的功能
#插座套接字物件的方法。
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() -- return the current hostname
#I獲取主機名的IP地址,必須傳一個引數
gethostbyname() -- map a hostname to its IP number
#IP地址或者主機的dns資訊,必須傳一個引數
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")
21
>>> print socket.getservbyname("http")
80
>>>
#協議名稱的數量?
getprotobyname() -- map a protocol name (e.g. `tcp`) to a number
exp:
>>> print socket.getprotobyname("tcp")
6
#從網路主機轉換16/32的位元組順序
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
#32-bit的包格式轉換成字串(123.45.67.89)
inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
#安全socket sll
ssl() -- secure socket layer support (only available if configured)
#獲取預設的超時值
socket.getdefaulttimeout() -- get the default timeout value
#設定預設的超時值,超時後程式自毀
socket.setdefaulttimeout() -- set the default timeout value
#連線到一個地址,可選的一個超時值
create_connection() -- connects to an address, with an optional timeout
[*] not available on all platforms!
Special objects:
SocketType -- type object for socket 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 (first argument to socket() call)
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
Many other constants may be defined; these may be used in calls to
the setsockopt() and getsockopt() methods.
CLASSES
__builtin__.object
_socketobject
_socketobject
exceptions.IOError(exceptions.EnvironmentError)
error
gaierror
herror
timeout
SocketType = class _socketobject(__builtin__.object)
| socket([family[, type[, proto]]]) -> socket object
|
#開啟一個 給定型別的socket.family指定地址族,他預設是AF_INET,type型別
是一個流,預設SOCK_STREAM(tcp),或者資料流SOCK_DGRAM(udp),這個protocol
預設引數是0,關鍵字引數都可以接收。
| 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 is 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物件的方法(關鍵字引數不允許)
| Methods of socket objects (keyword arguments not allowed):
| accept() -- accept a connection, returning new socket and client address
#接受一個連線,返回一個新的socket和客戶端地址。
exp:
print出來一個二元元組,一般進行拆分
(<socket._socketobject object at 0xb7791d84>, (`127.0.0.1`, 48336))
| bind(addr) -- bind the socket to a local address
#繫結一個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 an exception
#連線,返回一個錯誤程式碼而不是異常。
exp:
>>> s.connect_ex(("www.baidu.com",80))
0
>>> s.connect_ex(("www.caokbkbkbkbkkbkb.com",80))
106
>>> 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
# 返回低層的檔案描述符
>>> print s.fileno()
3
| getpeername() -- return remote address [*]
#返回遠端地址
| getsockname() -- return local address
>>> print s.getpeername() 剛返回的是百度的
(`61.135.169.125`, 80)
| getsockopt(level, optname[, buflen]) -- get socket options
#獲取socket選項
| gettimeout() -- return timeout or None
#返回timeout()超時值
| 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)
#接收資料,資訊來自緩衝區
| recvfrom(buflen[, flags]) -- receive data and sender`s address
#輸出資料和傳送者的地址
| recvfrom_into(buffer[, nbytes, [, flags])
| -- receive data and sender`s address (into a buffer)
#輸出資料和傳送者的地址,資訊來自緩衝區
| sendall(data[, flags]) -- send all data
#傳送所有資料。
| send(data[, flags]) -- send data, may not send all of it
#傳送資料時,可能不會傳送所有
| 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 an 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 local address. 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 after this 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.baidu.com",80))
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 when 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 the address of the remote endpoint. For IP sockets, the address
| info is a pair (hostaddr, port).
|
| getsockname(self, *args)
| getsockname() -> address info
|
| Return the address of 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. See 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 be at
| least 1; it specifies the number of unaccepted connection that the system
| will allow before refusing new connections.
|
| makefile(self, mode=`r`, bufsize=-1)
| makefile([mode[, bufsize]]) -> file object
|
| Return a regular 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, see 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. See the Unix manual for level and option.
| The value argument can either be 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
| of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).