~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Doc/library/socket.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-07-06 16:52:42 UTC
  • mfrom: (1.2.1 upstream) (2.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100706165242-2xv4i019r3et6c0j
Tags: 3.1.2+20100706-1ubuntu1
* Merge with Debian; remaining changes:
  - Regenerate the control file.
  - Add debian/patches/overwrite-semaphore-check for Lucid buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
and out-of-memory conditions can be raised; errors related to socket or address
91
91
semantics raise the error :exc:`socket.error`.
92
92
 
93
 
Non-blocking mode is supported through :meth:`setblocking`.  A generalization of
94
 
this based on timeouts is supported through :meth:`settimeout`.
 
93
Non-blocking mode is supported through :meth:`~socket.setblocking`.  A
 
94
generalization of this based on timeouts is supported through
 
95
:meth:`~socket.settimeout`.
95
96
 
96
97
The module :mod:`socket` exports the following constants and functions:
97
98
 
204
205
   :func:`getdefaulttimeout` is used.
205
206
 
206
207
 
207
 
.. function:: getaddrinfo(host, port[, family[, socktype[, proto[, flags]]]])
208
 
 
209
 
   Resolves the *host*/*port* argument, into a sequence of 5-tuples that contain
210
 
   all the necessary arguments for creating the corresponding socket. *host* is a domain
211
 
   name, a string representation of an IPv4/v6 address or ``None``. *port* is a string
212
 
   service name such as ``'http'``, a numeric port number or ``None``.
213
 
   The rest of the arguments are optional and must be numeric if specified.
214
 
   By passing ``None`` as the value of *host* and *port*, , you can pass ``NULL`` to the C API.
215
 
 
216
 
   The :func:`getaddrinfo` function returns a list of 5-tuples with the following
217
 
   structure:
 
208
.. function:: getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
 
209
 
 
210
   Translate the *host*/*port* argument into a sequence of 5-tuples that contain
 
211
   all the necessary arguments for creating a socket connected to that service.
 
212
   *host* is a domain name, a string representation of an IPv4/v6 address
 
213
   or ``None``. *port* is a string service name such as ``'http'``, a numeric
 
214
   port number or ``None``.  By passing ``None`` as the value of *host*
 
215
   and *port*, you can pass ``NULL`` to the underlying C API.
 
216
 
 
217
   The *family*, *socktype* and *proto* arguments can be optionally specified
 
218
   in order to narrow the list of addresses returned.  Passing zero as a
 
219
   value for each of these arguments selects the full range of results.
 
220
   The *flags* argument can be one or several of the ``AI_*`` constants,
 
221
   and will influence how results are computed and returned.
 
222
   For example, :const:`AI_NUMERICHOST` will disable domain name resolution
 
223
   and will raise an error if *host* is a domain name.
 
224
 
 
225
   The function returns a list of 5-tuples with the following structure:
218
226
 
219
227
   ``(family, socktype, proto, canonname, sockaddr)``
220
228
 
221
 
   *family*, *socktype*, *proto* are all integers and are meant to be passed to the
222
 
   :func:`socket` function. *canonname* is a string representing the canonical name
223
 
   of the *host*. It can be a numeric IPv4/v6 address when :const:`AI_CANONNAME` is
224
 
   specified for a numeric *host*. *sockaddr* is a tuple describing a socket
225
 
   address, as described above. See the source for :mod:`socket` and other
226
 
   library modules for a typical usage of the function.
227
 
 
 
229
   In these tuples, *family*, *socktype*, *proto* are all integers and are
 
230
   meant to be passed to the :func:`socket` function.  *canonname* will be
 
231
   a string representing the canonical name of the *host* if
 
232
   :const:`AI_CANONNAME` is part of the *flags* argument; else *canonname*
 
233
   will be empty.  *sockaddr* is a tuple describing a socket address, whose
 
234
   format depends on the returned *family* (a ``(address, port)`` 2-tuple for
 
235
   :const:`AF_INET`, a ``(address, port, flow info, scope id)`` 4-tuple for
 
236
   :const:`AF_INET6`), and is meant to be passed to the :meth:`socket.connect`
 
237
   method.
 
238
 
 
239
   The following example fetches address information for a hypothetical TCP
 
240
   connection to ``www.python.org`` on port 80 (results may differ on your
 
241
   system if IPv6 isn't enabled)::
 
242
 
 
243
      >>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)
 
244
      [(2, 1, 6, '', ('82.94.164.162', 80)),
 
245
       (10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]
228
246
 
229
247
.. function:: getfqdn([name])
230
248
 
553
571
   :platform: Windows
554
572
 
555
573
   The :meth:`ioctl` method is a limited interface to the WSAIoctl system
556
 
   interface. Please refer to the MSDN documentation for more information.
 
574
   interface.  Please refer to the `Win32 documentation
 
575
   <http://msdn.microsoft.com/en-us/library/ms741621%28VS.85%29.aspx>`_ for more
 
576
   information.
557
577
 
558
578
   On other platforms, the generic :func:`fcntl.fcntl` and :func:`fcntl.ioctl`
559
579
   functions may be used; they accept a socket object as their first argument.
656
676
   blocking mode.  In non-blocking mode, if a :meth:`recv` call doesn't find any
657
677
   data, or if a :meth:`send` call can't immediately dispose of the data, a
658
678
   :exc:`error` exception is raised; in blocking mode, the calls block until they
659
 
   can proceed. ``s.setblocking(0)`` is equivalent to ``s.settimeout(0)``;
 
679
   can proceed. ``s.setblocking(0)`` is equivalent to ``s.settimeout(0.0)``;
660
680
   ``s.setblocking(1)`` is equivalent to ``s.settimeout(None)``.
661
681
 
662
682
 
685
705
non-blocking mode, operations fail (with an error that is unfortunately
686
706
system-dependent) if they cannot be completed immediately.  In timeout mode,
687
707
operations fail if they cannot be completed within the timeout specified for the
688
 
socket or if the system returns an error.  The :meth:`setblocking` method is simply
689
 
a shorthand for certain :meth:`settimeout` calls.
 
708
socket or if the system returns an error.  The :meth:`~socket.setblocking`
 
709
method is simply a shorthand for certain :meth:`~socket.settimeout` calls.
690
710
 
691
711
Timeout mode internally sets the socket in non-blocking mode.  The blocking and
692
712
timeout modes are shared between file descriptors and socket objects that refer
693
713
to the same network endpoint.  A consequence of this is that file objects
694
 
returned by the :meth:`makefile` method must only be used when the socket is in
695
 
blocking mode; in timeout or non-blocking mode file operations that cannot be
696
 
completed immediately will fail.
 
714
returned by the :meth:`~socket.makefile` method must only be used when the
 
715
socket is in blocking mode; in timeout or non-blocking mode file operations
 
716
that cannot be completed immediately will fail.
697
717
 
698
 
Note that the :meth:`connect` operation is subject to the timeout setting, and
699
 
in general it is recommended to call :meth:`settimeout` before calling
700
 
:meth:`connect` or pass a timeout parameter to :meth:`create_connection`.
701
 
The system network stack may return a connection timeout error
702
 
of its own regardless of any Python socket timeout setting.
 
718
Note that the :meth:`~socket.connect` operation is subject to the timeout
 
719
setting, and in general it is recommended to call :meth:`~socket.settimeout`
 
720
before calling :meth:`~socket.connect` or pass a timeout parameter to
 
721
:meth:`create_connection`.  The system network stack may return a connection
 
722
timeout error of its own regardless of any Python socket timeout setting.
703
723
 
704
724
 
705
725
.. method:: socket.setsockopt(level, optname, value)
721
741
   are disallowed.  If *how* is :const:`SHUT_RDWR`, further sends and receives are
722
742
   disallowed.
723
743
 
724
 
Note that there are no methods :meth:`read` or :meth:`write`; use :meth:`recv`
725
 
and :meth:`send` without *flags* argument instead.
 
744
Note that there are no methods :meth:`read` or :meth:`write`; use
 
745
:meth:`~socket.recv` and :meth:`~socket.send` without *flags* argument instead.
726
746
 
727
747
Socket objects also have these (read-only) attributes that correspond to the
728
748
values given to the :class:`socket` constructor.
751
771
Here are four minimal example programs using the TCP/IP protocol: a server that
752
772
echoes all data that it receives back (servicing only one client), and a client
753
773
using it.  Note that a server must perform the sequence :func:`socket`,
754
 
:meth:`bind`, :meth:`listen`, :meth:`accept` (possibly repeating the
755
 
:meth:`accept` to service more than one client), while a client only needs the
756
 
sequence :func:`socket`, :meth:`connect`.  Also note that the server does not
757
 
:meth:`send`/:meth:`recv` on the  socket it is listening on but on the new
758
 
socket returned by :meth:`accept`.
 
774
:meth:`~socket.bind`, :meth:`~socket.listen`, :meth:`~socket.accept` (possibly
 
775
repeating the :meth:`~socket.accept` to service more than one client), while a
 
776
client only needs the sequence :func:`socket`, :meth:`~socket.connect`.  Also
 
777
note that the server does not :meth:`~socket.send`/:meth:`~socket.recv` on the
 
778
socket it is listening on but on the new socket returned by
 
779
:meth:`~socket.accept`.
759
780
 
760
781
The first two examples support IPv4 only. ::
761
782