Changeset 391 for python/trunk/Doc/howto/sockets.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/howto/sockets.rst
r2 r391 1 .. _socket-howto: 2 1 3 **************************** 2 4 Socket Programming HOWTO … … 17 19 Sockets 18 20 ======= 19 20 Sockets are used nearly everywhere, but are one of the most severely21 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 things23 working. It doesn't cover the fine points (and there are a lot of them), but I24 hope it will give you enough background to begin using them decently.25 21 26 22 I'm only going to talk about INET sockets, but they account for at least 99% of … … 44 40 ------- 45 41 46 Of the various forms of IPC (*Inter Process Communication*), sockets are by far47 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.42 Of the various forms of :abbr:`IPC (Inter Process Communication)`, 43 sockets are by far the most popular. On any given platform, there are 44 likely to be other forms of IPC that are faster, but for 45 cross-platform communication, sockets are about the only game in town. 50 46 51 47 They were invented in Berkeley as part of the BSD flavor of Unix. They spread … … 68 64 s.connect(("www.mcmillan-inc.com", 80)) 69 65 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). 66 When the ``connect`` completes, the socket ``s`` can be used to send 67 in a request for the text of the page. The same socket will read the 68 reply, and then be destroyed. That's right, destroyed. Client sockets 69 are normally only used for one exchange (or a small set of sequential 70 exchanges). 74 71 75 72 What happens in the web server is a bit more complex. First, the web server 76 creates a "server socket" .::73 creates a "server socket":: 77 74 78 75 #create an INET, STREAMing socket … … 86 83 87 84 A 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. 85 would be visible to the outside world. If we had used ``s.bind(('localhost', 86 80))`` or ``s.bind(('127.0.0.1', 80))`` we would still have a "server" socket, 87 but one that was only visible within the same machine. ``s.bind(('', 80))`` 88 specifies that the socket is reachable by any address the machine happens to 89 have. 91 90 92 91 A second thing to note: low number ports are usually reserved for "well known" … … 98 97 connections. If the rest of the code is written properly, that should be plenty. 99 98 100 OK, now we have a "server" socket, listening on port 80. Now weenter the99 Now that we have a "server" socket, listening on port 80, we can enter the 101 100 mainloop of the web server:: 102 101 … … 147 146 Now there are two sets of verbs to use for communication. You can use ``send`` 148 147 and ``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 theirsockets.148 use ``read`` and ``write``. The latter is the way Java presents its sockets. 150 149 I'm not going to talk about it here, except to warn you that you need to use 151 150 ``flush`` on sockets. These are buffered "files", and a common mistake is to … … 154 153 your output buffer. 155 154 156 Now we come t he major stumbling block of sockets - ``send`` and ``recv`` operate155 Now we come to the major stumbling block of sockets - ``send`` and ``recv`` operate 157 156 on the network buffers. They do not necessarily handle all the bytes you hand 158 157 them (or expect from them), because their major focus is handling the network … … 165 164 the process of closing) the connection. You will not receive any more data on 166 165 this connection. Ever. You may be able to send data successfully; I'll talk 167 about that some on the next page.166 more about this later. 168 167 169 168 A 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 that169 request, then reads a reply. That's it. The socket is discarded. This means that 171 170 a client can detect the end of the reply by receiving 0 bytes. 172 171 173 172 But 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 socket173 that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a socket 175 174 ``send`` or ``recv`` returns after handling 0 bytes, the connection has been 176 175 broken. If the connection has *not* been broken, you may wait on a ``recv`` … … 315 314 ==================== 316 315 317 If you've understood the prece eding, you already know most of what you need to316 If you've understood the preceding, you already know most of what you need to 318 317 know about the mechanics of using sockets. You'll still use the same calls, in 319 318 much the same ways. It's just that, if you do it right, your app will be almost … … 338 337 In C, coding ``select`` is fairly complex. In Python, it's a piece of cake, but 339 338 it's close enough to the C version that if you understand ``select`` in Python, 340 you'll have little trouble with it in C .::339 you'll have little trouble with it in C:: 341 340 342 341 ready_to_read, ready_to_write, in_error = \ … … 355 354 reason to do otherwise. 356 355 357 In return, you will get three lists. They havethe sockets that are actually356 In return, you will get three lists. They contain the sockets that are actually 358 357 readable, 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. 358 empty) of the corresponding list you passed in. 361 359 362 360 If a socket is in the output readable list, you can be
Note:
See TracChangeset
for help on using the changeset viewer.