PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/diff/socket.rst.diff

https://bitbucket.org/pydocja/py26
diff | 409 lines | 312 code | 97 blank | 0 comment | 0 complexity | c7169594368367d7c2f55c20010ab937 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. --- r262/library/socket.rst 2009-01-04 06:55:17.853888000 +0900
  2. +++ r266/library/socket.rst 2010-06-12 18:50:02.725360000 +0900
  3. @@ -1,13 +1,12 @@
  4. -
  5. :mod:`socket` --- Low-level networking interface
  6. ================================================
  7. .. module:: socket
  8. :synopsis: Low-level networking interface.
  9. This module provides access to the BSD *socket* interface. It is available on
  10. all modern Unix systems, Windows, Mac OS X, BeOS, OS/2, and probably additional
  11. platforms.
  12. .. note::
  13. @@ -64,44 +63,45 @@
  14. numeric address in *host* portion.
  15. .. versionadded:: 2.5
  16. AF_NETLINK sockets are represented as pairs ``pid, groups``.
  17. .. versionadded:: 2.6
  18. Linux-only support for TIPC is also available using the :const:`AF_TIPC`
  19. address family. TIPC is an open, non-IP based networked protocol designed
  20. for use in clustered computer environments. Addresses are represented by a
  21. tuple, and the fields depend on the address type. The general tuple form is
  22. ``(addr_type, v1, v2, v3 [, scope])``, where:
  23. - - *addr_type* is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
  24. - TIPC_ADDR_ID.
  25. - - *scope* is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
  26. - TIPC_NODE_SCOPE.
  27. - - If *addr_type* is TIPC_ADDR_NAME, then *v1* is the server type, *v2* is
  28. - the port identifier, and *v3* should be 0.
  29. + - *addr_type* is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
  30. + TIPC_ADDR_ID.
  31. + - *scope* is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
  32. + TIPC_NODE_SCOPE.
  33. + - If *addr_type* is TIPC_ADDR_NAME, then *v1* is the server type, *v2* is
  34. + the port identifier, and *v3* should be 0.
  35. - If *addr_type* is TIPC_ADDR_NAMESEQ, then *v1* is the server type, *v2*
  36. - is the lower port number, and *v3* is the upper port number.
  37. + If *addr_type* is TIPC_ADDR_NAMESEQ, then *v1* is the server type, *v2*
  38. + is the lower port number, and *v3* is the upper port number.
  39. - If *addr_type* is TIPC_ADDR_ID, then *v1* is the node, *v2* is the
  40. - reference, and *v3* should be set to 0.
  41. + If *addr_type* is TIPC_ADDR_ID, then *v1* is the node, *v2* is the
  42. + reference, and *v3* should be set to 0.
  43. All errors raise exceptions. The normal exceptions for invalid argument types
  44. and out-of-memory conditions can be raised; errors related to socket or address
  45. semantics raise the error :exc:`socket.error`.
  46. -Non-blocking mode is supported through :meth:`setblocking`. A generalization of
  47. -this based on timeouts is supported through :meth:`settimeout`.
  48. +Non-blocking mode is supported through :meth:`~socket.setblocking`. A
  49. +generalization of this based on timeouts is supported through
  50. +:meth:`~socket.settimeout`.
  51. The module :mod:`socket` exports the following constants and functions:
  52. .. exception:: error
  53. .. index:: module: errno
  54. This exception is raised for socket-related errors. The accompanying value is
  55. either a string telling what went wrong or a pair ``(errno, string)``
  56. representing an error returned by a system call, similar to the value
  57. accompanying :exc:`os.error`. See the module :mod:`errno`, which contains names
  58. @@ -207,44 +207,62 @@
  59. .. function:: create_connection(address[, timeout])
  60. Convenience function. Connect to *address* (a 2-tuple ``(host, port)``),
  61. and return the socket object. Passing the optional *timeout* parameter will
  62. set the timeout on the socket instance before attempting to connect. If no
  63. *timeout* is supplied, the global default timeout setting returned by
  64. :func:`getdefaulttimeout` is used.
  65. .. versionadded:: 2.6
  66. -.. function:: getaddrinfo(host, port[, family[, socktype[, proto[, flags]]]])
  67. +.. function:: getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
  68. - Resolves the *host*/*port* argument, into a sequence of 5-tuples that contain
  69. - all the necessary arguments for creating the corresponding socket. *host* is a domain
  70. - name, a string representation of an IPv4/v6 address or ``None``. *port* is a string
  71. - service name such as ``'http'``, a numeric port number or ``None``.
  72. - The rest of the arguments are optional and must be numeric if specified.
  73. - By passing ``None`` as the value of *host* and *port*, , you can pass ``NULL`` to the C API.
  74. + Translate the *host*/*port* argument into a sequence of 5-tuples that contain
  75. + all the necessary arguments for creating a socket connected to that service.
  76. + *host* is a domain name, a string representation of an IPv4/v6 address
  77. + or ``None``. *port* is a string service name such as ``'http'``, a numeric
  78. + port number or ``None``. By passing ``None`` as the value of *host*
  79. + and *port*, you can pass ``NULL`` to the underlying C API.
  80. +
  81. + The *family*, *socktype* and *proto* arguments can be optionally specified
  82. + in order to narrow the list of addresses returned. Passing zero as a
  83. + value for each of these arguments selects the full range of results.
  84. + The *flags* argument can be one or several of the ``AI_*`` constants,
  85. + and will influence how results are computed and returned.
  86. + For example, :const:`AI_NUMERICHOST` will disable domain name resolution
  87. + and will raise an error if *host* is a domain name.
  88. - The :func:`getaddrinfo` function returns a list of 5-tuples with the following
  89. - structure:
  90. + The function returns a list of 5-tuples with the following structure:
  91. ``(family, socktype, proto, canonname, sockaddr)``
  92. - *family*, *socktype*, *proto* are all integers and are meant to be passed to the
  93. - :func:`socket` function. *canonname* is a string representing the canonical name
  94. - of the *host*. It can be a numeric IPv4/v6 address when :const:`AI_CANONNAME` is
  95. - specified for a numeric *host*. *sockaddr* is a tuple describing a socket
  96. - address, as described above. See the source for :mod:`socket` and other
  97. - library modules for a typical usage of the function.
  98. + In these tuples, *family*, *socktype*, *proto* are all integers and are
  99. + meant to be passed to the :func:`socket` function. *canonname* will be
  100. + a string representing the canonical name of the *host* if
  101. + :const:`AI_CANONNAME` is part of the *flags* argument; else *canonname*
  102. + will be empty. *sockaddr* is a tuple describing a socket address, whose
  103. + format depends on the returned *family* (a ``(address, port)`` 2-tuple for
  104. + :const:`AF_INET`, a ``(address, port, flow info, scope id)`` 4-tuple for
  105. + :const:`AF_INET6`), and is meant to be passed to the :meth:`socket.connect`
  106. + method.
  107. +
  108. + The following example fetches address information for a hypothetical TCP
  109. + connection to ``www.python.org`` on port 80 (results may differ on your
  110. + system if IPv6 isn't enabled)::
  111. +
  112. + >>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)
  113. + [(2, 1, 6, '', ('82.94.164.162', 80)),
  114. + (10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]
  115. .. versionadded:: 2.2
  116. .. function:: getfqdn([name])
  117. Return a fully qualified domain name for *name*. If *name* is omitted or empty,
  118. it is interpreted as the local host. To find the fully qualified name, the
  119. hostname returned by :func:`gethostbyaddr` is checked, followed by aliases for the
  120. host, if available. The first name which includes a period is selected. In
  121. case no fully qualified domain name is available, the hostname as returned by
  122. :func:`gethostname` is returned.
  123. @@ -392,43 +410,46 @@
  124. where the host byte order is the same as network byte order, this is a no-op;
  125. otherwise, it performs a 2-byte swap operation.
  126. .. function:: inet_aton(ip_string)
  127. Convert an IPv4 address from dotted-quad string format (for example,
  128. '123.45.67.89') to 32-bit packed binary format, as a string four characters in
  129. length. This is useful when conversing with a program that uses the standard C
  130. library and needs objects of type :ctype:`struct in_addr`, which is the C type
  131. for the 32-bit packed binary this function returns.
  132. + :func:`inet_aton` also accepts strings with less than three dots; see the
  133. + Unix manual page :manpage:`inet(3)` for details.
  134. +
  135. If the IPv4 address string passed to this function is invalid,
  136. :exc:`socket.error` will be raised. Note that exactly what is valid depends on
  137. the underlying C implementation of :cfunc:`inet_aton`.
  138. - :func:`inet_aton` does not support IPv6, and :func:`getnameinfo` should be used
  139. + :func:`inet_aton` does not support IPv6, and :func:`inet_pton` should be used
  140. instead for IPv4/v6 dual stack support.
  141. .. function:: inet_ntoa(packed_ip)
  142. Convert a 32-bit packed IPv4 address (a string four characters in length) to its
  143. standard dotted-quad string representation (for example, '123.45.67.89'). This
  144. is useful when conversing with a program that uses the standard C library and
  145. needs objects of type :ctype:`struct in_addr`, which is the C type for the
  146. 32-bit packed binary data this function takes as an argument.
  147. If the string passed to this function is not exactly 4 bytes in length,
  148. :exc:`socket.error` will be raised. :func:`inet_ntoa` does not support IPv6, and
  149. - :func:`getnameinfo` should be used instead for IPv4/v6 dual stack support.
  150. + :func:`inet_ntop` should be used instead for IPv4/v6 dual stack support.
  151. .. function:: inet_pton(address_family, ip_string)
  152. Convert an IP address from its family-specific string format to a packed, binary
  153. format. :func:`inet_pton` is useful when a library or network protocol calls for
  154. an object of type :ctype:`struct in_addr` (similar to :func:`inet_aton`) or
  155. :ctype:`struct in6_addr`.
  156. Supported values for *address_family* are currently :const:`AF_INET` and
  157. :const:`AF_INET6`. If the IP address string *ip_string* is invalid,
  158. :exc:`socket.error` will be raised. Note that exactly what is valid depends on
  159. @@ -585,25 +606,30 @@
  160. and its integer value is returned by the function. If *buflen* is present, it
  161. specifies the maximum length of the buffer used to receive the option in, and
  162. this buffer is returned as a string. It is up to the caller to decode the
  163. contents of the buffer (see the optional built-in module :mod:`struct` for a way
  164. to decode C structures encoded as strings).
  165. .. method:: socket.ioctl(control, option)
  166. :platform: Windows
  167. The :meth:`ioctl` method is a limited interface to the WSAIoctl system
  168. - interface. Please refer to the MSDN documentation for more information.
  169. + interface. Please refer to the `Win32 documentation
  170. + <http://msdn.microsoft.com/en-us/library/ms741621%28VS.85%29.aspx>`_ for more
  171. + information.
  172. +
  173. + On other platforms, the generic :func:`fcntl.fcntl` and :func:`fcntl.ioctl`
  174. + functions may be used; they accept a socket object as their first argument.
  175. .. versionadded:: 2.6
  176. .. method:: socket.listen(backlog)
  177. Listen for connections made to the socket. The *backlog* argument specifies the
  178. maximum number of queued connections and should be at least 1; the maximum value
  179. is system-dependent (usually 5).
  180. .. method:: socket.makefile([mode[, bufsize]])
  181. @@ -647,28 +673,28 @@
  182. new string. The return value is a pair ``(nbytes, address)`` where *nbytes* is
  183. the number of bytes received and *address* is the address of the socket sending
  184. the data. See the Unix manual page :manpage:`recv(2)` for the meaning of the
  185. optional argument *flags*; it defaults to zero. (The format of *address*
  186. depends on the address family --- see above.)
  187. .. versionadded:: 2.5
  188. .. method:: socket.recv_into(buffer[, nbytes[, flags]])
  189. Receive up to *nbytes* bytes from the socket, storing the data into a buffer
  190. - rather than creating a new string. If *nbytes* is not specified (or 0),
  191. - receive up to the size available in the given buffer. See the Unix manual page
  192. - :manpage:`recv(2)` for the meaning of the optional argument *flags*; it defaults
  193. - to zero.
  194. + rather than creating a new string. If *nbytes* is not specified (or 0),
  195. + receive up to the size available in the given buffer. Returns the number of
  196. + bytes received. See the Unix manual page :manpage:`recv(2)` for the meaning
  197. + of the optional argument *flags*; it defaults to zero.
  198. .. versionadded:: 2.5
  199. .. method:: socket.send(string[, flags])
  200. Send data to the socket. The socket must be connected to a remote socket. The
  201. optional *flags* argument has the same meaning as for :meth:`recv` above.
  202. Returns the number of bytes sent. Applications are responsible for checking that
  203. all data has been sent; if only some of the data was transmitted, the
  204. application needs to attempt delivery of the remaining data.
  205. @@ -690,25 +716,25 @@
  206. argument has the same meaning as for :meth:`recv` above. Return the number of
  207. bytes sent. (The format of *address* depends on the address family --- see
  208. above.)
  209. .. method:: socket.setblocking(flag)
  210. Set blocking or non-blocking mode of the socket: if *flag* is 0, the socket is
  211. set to non-blocking, else to blocking mode. Initially all sockets are in
  212. blocking mode. In non-blocking mode, if a :meth:`recv` call doesn't find any
  213. data, or if a :meth:`send` call can't immediately dispose of the data, a
  214. :exc:`error` exception is raised; in blocking mode, the calls block until they
  215. - can proceed. ``s.setblocking(0)`` is equivalent to ``s.settimeout(0)``;
  216. + can proceed. ``s.setblocking(0)`` is equivalent to ``s.settimeout(0.0)``;
  217. ``s.setblocking(1)`` is equivalent to ``s.settimeout(None)``.
  218. .. method:: socket.settimeout(value)
  219. Set a timeout on blocking socket operations. The *value* argument can be a
  220. nonnegative float expressing seconds, or ``None``. If a float is given,
  221. subsequent socket operations will raise an :exc:`timeout` exception if the
  222. timeout period *value* has elapsed before the operation has completed. Setting
  223. a timeout of ``None`` disables timeouts on socket operations.
  224. ``s.settimeout(0.0)`` is equivalent to ``s.setblocking(0)``;
  225. ``s.settimeout(None)`` is equivalent to ``s.setblocking(1)``.
  226. @@ -717,64 +743,67 @@
  227. .. method:: socket.gettimeout()
  228. Return the timeout in floating seconds associated with socket operations, or
  229. ``None`` if no timeout is set. This reflects the last call to
  230. :meth:`setblocking` or :meth:`settimeout`.
  231. .. versionadded:: 2.3
  232. Some notes on socket blocking and timeouts: A socket object can be in one of
  233. three modes: blocking, non-blocking, or timeout. Sockets are always created in
  234. -blocking mode. In blocking mode, operations block until complete. In
  235. +blocking mode. In blocking mode, operations block until complete or
  236. +the system returns an error (such as connection timed out). In
  237. non-blocking mode, operations fail (with an error that is unfortunately
  238. system-dependent) if they cannot be completed immediately. In timeout mode,
  239. operations fail if they cannot be completed within the timeout specified for the
  240. -socket. The :meth:`setblocking` method is simply a shorthand for certain
  241. -:meth:`settimeout` calls.
  242. +socket or if the system returns an error. The :meth:`~socket.setblocking`
  243. +method is simply a shorthand for certain :meth:`~socket.settimeout` calls.
  244. Timeout mode internally sets the socket in non-blocking mode. The blocking and
  245. timeout modes are shared between file descriptors and socket objects that refer
  246. to the same network endpoint. A consequence of this is that file objects
  247. -returned by the :meth:`makefile` method must only be used when the socket is in
  248. -blocking mode; in timeout or non-blocking mode file operations that cannot be
  249. -completed immediately will fail.
  250. -
  251. -Note that the :meth:`connect` operation is subject to the timeout setting, and
  252. -in general it is recommended to call :meth:`settimeout` before calling
  253. -:meth:`connect`.
  254. +returned by the :meth:`~socket.makefile` method must only be used when the
  255. +socket is in blocking mode; in timeout or non-blocking mode file operations
  256. +that cannot be completed immediately will fail.
  257. +
  258. +Note that the :meth:`~socket.connect` operation is subject to the timeout
  259. +setting, and in general it is recommended to call :meth:`~socket.settimeout`
  260. +before calling :meth:`~socket.connect` or pass a timeout parameter to
  261. +:meth:`create_connection`. The system network stack may return a connection
  262. +timeout error of its own regardless of any Python socket timeout setting.
  263. .. method:: socket.setsockopt(level, optname, value)
  264. .. index:: module: struct
  265. Set the value of the given socket option (see the Unix manual page
  266. :manpage:`setsockopt(2)`). The needed symbolic constants are defined in the
  267. :mod:`socket` module (:const:`SO_\*` etc.). The value can be an integer or a
  268. string representing a buffer. In the latter case it is up to the caller to
  269. ensure that the string contains the proper bits (see the optional built-in
  270. module :mod:`struct` for a way to encode C structures as strings).
  271. .. method:: socket.shutdown(how)
  272. Shut down one or both halves of the connection. If *how* is :const:`SHUT_RD`,
  273. further receives are disallowed. If *how* is :const:`SHUT_WR`, further sends
  274. are disallowed. If *how* is :const:`SHUT_RDWR`, further sends and receives are
  275. disallowed.
  276. -Note that there are no methods :meth:`read` or :meth:`write`; use :meth:`recv`
  277. -and :meth:`send` without *flags* argument instead.
  278. +Note that there are no methods :meth:`read` or :meth:`write`; use
  279. +:meth:`~socket.recv` and :meth:`~socket.send` without *flags* argument instead.
  280. Socket objects also have these (read-only) attributes that correspond to the
  281. values given to the :class:`socket` constructor.
  282. .. attribute:: socket.family
  283. The socket family.
  284. .. versionadded:: 2.5
  285. @@ -791,29 +820,30 @@
  286. .. versionadded:: 2.5
  287. .. _socket-example:
  288. Example
  289. -------
  290. Here are four minimal example programs using the TCP/IP protocol: a server that
  291. echoes all data that it receives back (servicing only one client), and a client
  292. using it. Note that a server must perform the sequence :func:`socket`,
  293. -:meth:`bind`, :meth:`listen`, :meth:`accept` (possibly repeating the
  294. -:meth:`accept` to service more than one client), while a client only needs the
  295. -sequence :func:`socket`, :meth:`connect`. Also note that the server does not
  296. -:meth:`send`/:meth:`recv` on the socket it is listening on but on the new
  297. -socket returned by :meth:`accept`.
  298. +:meth:`~socket.bind`, :meth:`~socket.listen`, :meth:`~socket.accept` (possibly
  299. +repeating the :meth:`~socket.accept` to service more than one client), while a
  300. +client only needs the sequence :func:`socket`, :meth:`~socket.connect`. Also
  301. +note that the server does not :meth:`~socket.send`/:meth:`~socket.recv` on the
  302. +socket it is listening on but on the new socket returned by
  303. +:meth:`~socket.accept`.
  304. The first two examples support IPv4 only. ::
  305. # Echo server program
  306. import socket
  307. HOST = '' # Symbolic name meaning all available interfaces
  308. PORT = 50007 # Arbitrary non-privileged port
  309. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  310. s.bind((HOST, PORT))
  311. s.listen(1)
  312. conn, addr = s.accept()