Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/howto/sockets.rst

    r2 r391  
     1.. _socket-howto:
     2
    13****************************
    24  Socket Programming HOWTO
     
    1719Sockets
    1820=======
    19 
    20 Sockets are used nearly everywhere, but are one of the most severely
    21 misunderstood technologies around. This is a 10,000 foot overview of sockets.
    22 It's not really a tutorial - you'll still have work to do in getting things
    23 working. It doesn't cover the fine points (and there are a lot of them), but I
    24 hope it will give you enough background to begin using them decently.
    2521
    2622I'm only going to talk about INET sockets, but they account for at least 99% of
     
    4440-------
    4541
    46 Of the various forms of IPC (*Inter Process Communication*), sockets are by far
    47 the most popular.  On any given platform, there are likely to be other forms of
    48 IPC that are faster, but for cross-platform communication, sockets are about the
    49 only game in town.
     42Of the various forms of :abbr:`IPC (Inter Process Communication)`,
     43sockets are by far the most popular.  On any given platform, there are
     44likely to be other forms of IPC that are faster, but for
     45cross-platform communication, sockets are about the only game in town.
    5046
    5147They were invented in Berkeley as part of the BSD flavor of Unix. They spread
     
    6864   s.connect(("www.mcmillan-inc.com", 80))
    6965
    70 When the ``connect`` completes, the socket ``s`` can now be used to send in a
    71 request for the text of this page. The same socket will read the reply, and then
    72 be destroyed. That's right - destroyed. Client sockets are normally only used
    73 for one exchange (or a small set of sequential exchanges).
     66When the ``connect`` completes, the socket ``s`` can be used to send
     67in a request for the text of the page. The same socket will read the
     68reply, and then be destroyed. That's right, destroyed. Client sockets
     69are normally only used for one exchange (or a small set of sequential
     70exchanges).
    7471
    7572What happens in the web server is a bit more complex. First, the web server
    76 creates a "server socket". ::
     73creates a "server socket"::
    7774
    7875   #create an INET, STREAMing socket
     
    8683
    8784A couple things to notice: we used ``socket.gethostname()`` so that the socket
    88 would be visible to the outside world. If we had used ``s.bind(('', 80))`` or
    89 ``s.bind(('localhost', 80))`` or ``s.bind(('127.0.0.1', 80))`` we would still
    90 have a "server" socket, but one that was only visible within the same machine.
     85would be visible to the outside world.  If we had used ``s.bind(('localhost',
     8680))`` or ``s.bind(('127.0.0.1', 80))`` we would still have a "server" socket,
     87but one that was only visible within the same machine.  ``s.bind(('', 80))``
     88specifies that the socket is reachable by any address the machine happens to
     89have.
    9190
    9291A second thing to note: low number ports are usually reserved for "well known"
     
    9897connections. If the rest of the code is written properly, that should be plenty.
    9998
    100 OK, now we have a "server" socket, listening on port 80. Now we enter the
     99Now that we have a "server" socket, listening on port 80, we can enter the
    101100mainloop of the web server::
    102101
     
    147146Now there are two sets of verbs to use for communication. You can use ``send``
    148147and ``recv``, or you can transform your client socket into a file-like beast and
    149 use ``read`` and ``write``. The latter is the way Java presents their sockets.
     148use ``read`` and ``write``. The latter is the way Java presents its sockets.
    150149I'm not going to talk about it here, except to warn you that you need to use
    151150``flush`` on sockets. These are buffered "files", and a common mistake is to
     
    154153your output buffer.
    155154
    156 Now we come the major stumbling block of sockets - ``send`` and ``recv`` operate
     155Now we come to the major stumbling block of sockets - ``send`` and ``recv`` operate
    157156on the network buffers. They do not necessarily handle all the bytes you hand
    158157them (or expect from them), because their major focus is handling the network
     
    165164the process of closing) the connection.  You will not receive any more data on
    166165this connection. Ever.  You may be able to send data successfully; I'll talk
    167 about that some on the next page.
     166more about this later.
    168167
    169168A protocol like HTTP uses a socket for only one transfer. The client sends a
    170 request, the reads a reply.  That's it. The socket is discarded. This means that
     169request, then reads a reply.  That's it. The socket is discarded. This means that
    171170a client can detect the end of the reply by receiving 0 bytes.
    172171
    173172But if you plan to reuse your socket for further transfers, you need to realize
    174 that *there is no "EOT" (End of Transfer) on a socket.* I repeat: if a socket
     173that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a socket
    175174``send`` or ``recv`` returns after handling 0 bytes, the connection has been
    176175broken.  If the connection has *not* been broken, you may wait on a ``recv``
     
    315314====================
    316315
    317 If you've understood the preceeding, you already know most of what you need to
     316If you've understood the preceding, you already know most of what you need to
    318317know about the mechanics of using sockets. You'll still use the same calls, in
    319318much the same ways. It's just that, if you do it right, your app will be almost
     
    338337In C, coding ``select`` is fairly complex. In Python, it's a piece of cake, but
    339338it's close enough to the C version that if you understand ``select`` in Python,
    340 you'll have little trouble with it in C. ::
     339you'll have little trouble with it in C::
    341340
    342341   ready_to_read, ready_to_write, in_error = \
     
    355354reason to do otherwise.
    356355
    357 In return, you will get three lists. They have the sockets that are actually
     356In return, you will get three lists. They contain the sockets that are actually
    358357readable, writable and in error. Each of these lists is a subset (possibly
    359 empty) of the corresponding list you passed in. And if you put a socket in more
    360 than one input list, it will only be (at most) in one output list.
     358empty) of the corresponding list you passed in.
    361359
    362360If a socket is in the output readable list, you can be
Note: See TracChangeset for help on using the changeset viewer.