python -- socket

时常翻译些python英文文档,,一方面学习python,一方面加强自己英语水平...

socket是应用层与传输层的编程接口~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Help on class _socketobject in module socket:
#在模块socket中_socketobject类帮助文档
class _socketobject(__builtin__.object)
|socket([family[, type[, proto]]]) -> socket object #socket对象,有三个参数

|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类型打开. family参数指定了地址族,默认值为AF_INET. type 参数指定了socket是数据流(SOCK_STREAM,默认值)还是数据报(SOCK_DGRAM). proto参数默认值为0, 指定了默认协议,关键词参数是可选的
#PS:书上说family参数指定的是通信类型,我认为其实就是指定网络层协议,默认值AF_INET表示的是IPv4协议...type指定的是协议家族,我认为是指定传输层协议,数据流表示的是TCP协议,数据报表示的是UDP协议...
|A socket object represents one endpoint of a network connection.
#一个socket对象代表一个网络连接的端点
|Methods of socket objects (keyword arguments not allowed):
#下面为的socket对象的方法(关键词参数不被允许使用)
|accept() -- accept a connection, returning new socket and client address # 等待接受一个新的连接,返回一个新的socket对象和客户端地址 都是在服务端使用该方法.
  |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
|dup() -- return a new socket object identical to the current one [*]
|fileno() -- return underlying file descriptor
|getpeername() -- return remote address [*] #返回一个远程地址
|getsockname() -- return local address #返回一个本地地址
|getsockopt(level, optname[, buflen]) -- get socket options
|gettimeout() -- return timeout or None
|listen(n) -- start listening for incoming connections #开始监听,用于服务器,传入一个参数n,指定了最大连接数
|makefile([mode, [bufsize]]) -- return a file object for the socket [*] #返回一个文件对象
|recv(buflen[, flags]) -- receive data #接收数据
|recv_into(buffer[, nbytes[, flags]]) -- receive datainto 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
|setsockopt(level, optname, value) -- set socket options
|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(...)
| 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, _closedsocket=<class 'socket._closedsocket'>,_delegate_methods=('recv', 'recvfrom', 'recv_into', 'recvfrom_into', 'send', 'sendto'), setattr=<built-in function setattr>)
| close()
| Close the socket. It cannot be used after this call.
|connect(...)
| connect(address)

| Connect the socket to a remote address. For IP sockets, the address
is a pair (host, port).

|connect_ex(...)
| connect_ex(address) -> errno

| 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(...)
| fileno() -> integer
Return the integer file descriptor of the socket.

|getpeername(...)
| getpeername() -> address info

Return the address of the remote endpoint. For IP sockets, the address
| info is a pair (hostaddr, port).

|getsockname(...)
| getsockname() -> address info

Return the address of the local endpoint. For IP sockets, the address
| info is a pair (hostaddr, port).

|getsockopt(...)
| 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(...)
| gettimeout() -> timeout

| Returns the timeout in seconds (float) associated with socket
| operations. A timeout of None indicates that timeouts on socket
| operations are disabled.

|listen(...)
| listen(backlog)

| Enable a server to accept connections. The backlog argument must be at
| least 0 (if it is lower, it is set to 0); it specifies the number of
| unaccepted connections 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(...)
| 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(...)
| 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(...)
| 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(...)
| 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(...)
| 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).
|-----------------------------------
|Data descriptors defined here:

|__weakref__
|list of weak references to the object (if defined)

|family
|the socket family

|proto
|the socket protocol

|recv

|recv_into

|recvfrom

|recvfrom_into

|send

|sendto

|type
|the socket type