Add missing 'self' parameters
[pytest.git] / Doc / howto / sockets.tex
blob0607a933b4d559a892906ee1ee85b67a3a5b571a
1 \documentclass{howto}
3 \title{Socket Programming HOWTO}
5 \release{0.00}
7 \author{Gordon McMillan}
8 \authoraddress{\email{gmcm@hypernet.com}}
10 \begin{document}
11 \maketitle
13 \begin{abstract}
14 \noindent
15 Sockets are used nearly everywhere, but are one of the most severely
16 misunderstood technologies around. This is a 10,000 foot overview of
17 sockets. It's not really a tutorial - you'll still have work to do in
18 getting things operational. It doesn't cover the fine points (and there
19 are a lot of them), but I hope it will give you enough background to
20 begin using them decently.
22 This document is available from the Python HOWTO page at
23 \url{http://www.python.org/doc/howto}.
25 \end{abstract}
27 \tableofcontents
29 \section{Sockets}
31 Sockets are used nearly everywhere, but are one of the most severely
32 misunderstood technologies around. This is a 10,000 foot overview of
33 sockets. It's not really a tutorial - you'll still have work to do in
34 getting things working. It doesn't cover the fine points (and there
35 are a lot of them), but I hope it will give you enough background to
36 begin using them decently.
38 I'm only going to talk about INET sockets, but they account for at
39 least 99\% of the sockets in use. And I'll only talk about STREAM
40 sockets - unless you really know what you're doing (in which case this
41 HOWTO isn't for you!), you'll get better behavior and performance from
42 a STREAM socket than anything else. I will try to clear up the mystery
43 of what a socket is, as well as some hints on how to work with
44 blocking and non-blocking sockets. But I'll start by talking about
45 blocking sockets. You'll need to know how they work before dealing
46 with non-blocking sockets.
48 Part of the trouble with understanding these things is that "socket"
49 can mean a number of subtly different things, depending on context. So
50 first, let's make a distinction between a "client" socket - an
51 endpoint of a conversation, and a "server" socket, which is more like
52 a switchboard operator. The client application (your browser, for
53 example) uses "client" sockets exclusively; the web server it's
54 talking to uses both "server" sockets and "client" sockets.
57 \subsection{History}
59 Of the various forms of IPC (\emph{Inter Process Communication}),
60 sockets are by far the most popular. On any given platform, there are
61 likely to be other forms of IPC that are faster, but for
62 cross-platform communication, sockets are about the only game in town.
64 They were invented in Berkeley as part of the BSD flavor of Unix. They
65 spread like wildfire with the Internet. With good reason --- the
66 combination of sockets with INET makes talking to arbitrary machines
67 around the world unbelievably easy (at least compared to other
68 schemes).
70 \section{Creating a Socket}
72 Roughly speaking, when you clicked on the link that brought you to
73 this page, your browser did something like the following:
75 \begin{verbatim}
76 #create an INET, STREAMing socket
77 s = socket.socket(
78 socket.AF_INET, socket.SOCK_STREAM)
79 #now connect to the web server on port 80
80 # - the normal http port
81 s.connect(("www.mcmillan-inc.com", 80))
82 \end{verbatim}
84 When the \code{connect} completes, the socket \code{s} can
85 now be used to send in a request for the text of this page. The same
86 socket will read the reply, and then be destroyed. That's right -
87 destroyed. Client sockets are normally only used for one exchange (or
88 a small set of sequential exchanges).
90 What happens in the web server is a bit more complex. First, the web
91 server creates a "server socket".
93 \begin{verbatim}
94 #create an INET, STREAMing socket
95 serversocket = socket.socket(
96 socket.AF_INET, socket.SOCK_STREAM)
97 #bind the socket to a public host,
98 # and a well-known port
99 serversocket.bind((socket.gethostname(), 80))
100 #become a server socket
101 serversocket.listen(5)
102 \end{verbatim}
104 A couple things to notice: we used \code{socket.gethostname()}
105 so that the socket would be visible to the outside world. If we had
106 used \code{s.bind(('', 80))} or \code{s.bind(('localhost',
107 80))} or \code{s.bind(('127.0.0.1', 80))} we would still
108 have a "server" socket, but one that was only visible within the same
109 machine.
111 A second thing to note: low number ports are usually reserved for
112 "well known" services (HTTP, SNMP etc). If you're playing around, use
113 a nice high number (4 digits).
115 Finally, the argument to \code{listen} tells the socket library that
116 we want it to queue up as many as 5 connect requests (the normal max)
117 before refusing outside connections. If the rest of the code is
118 written properly, that should be plenty.
120 OK, now we have a "server" socket, listening on port 80. Now we enter
121 the mainloop of the web server:
123 \begin{verbatim}
124 while 1:
125 #accept connections from outside
126 (clientsocket, address) = serversocket.accept()
127 #now do something with the clientsocket
128 #in this case, we'll pretend this is a threaded server
129 ct = client_thread(clientsocket)
130 ct.run()
131 \end{verbatim}
133 There's actually 3 general ways in which this loop could work -
134 dispatching a thread to handle \code{clientsocket}, create a new
135 process to handle \code{clientsocket}, or restructure this app
136 to use non-blocking sockets, and mulitplex between our "server" socket
137 and any active \code{clientsocket}s using
138 \code{select}. More about that later. The important thing to
139 understand now is this: this is \emph{all} a "server" socket
140 does. It doesn't send any data. It doesn't receive any data. It just
141 produces "client" sockets. Each \code{clientsocket} is created
142 in response to some \emph{other} "client" socket doing a
143 \code{connect()} to the host and port we're bound to. As soon as
144 we've created that \code{clientsocket}, we go back to listening
145 for more connections. The two "clients" are free to chat it up - they
146 are using some dynamically allocated port which will be recycled when
147 the conversation ends.
149 \subsection{IPC} If you need fast IPC between two processes
150 on one machine, you should look into whatever form of shared memory
151 the platform offers. A simple protocol based around shared memory and
152 locks or semaphores is by far the fastest technique.
154 If you do decide to use sockets, bind the "server" socket to
155 \code{'localhost'}. On most platforms, this will take a shortcut
156 around a couple of layers of network code and be quite a bit faster.
159 \section{Using a Socket}
161 The first thing to note, is that the web browser's "client" socket and
162 the web server's "client" socket are identical beasts. That is, this
163 is a "peer to peer" conversation. Or to put it another way, \emph{as the
164 designer, you will have to decide what the rules of etiquette are for
165 a conversation}. Normally, the \code{connect}ing socket
166 starts the conversation, by sending in a request, or perhaps a
167 signon. But that's a design decision - it's not a rule of sockets.
169 Now there are two sets of verbs to use for communication. You can use
170 \code{send} and \code{recv}, or you can transform your
171 client socket into a file-like beast and use \code{read} and
172 \code{write}. The latter is the way Java presents their
173 sockets. I'm not going to talk about it here, except to warn you that
174 you need to use \code{flush} on sockets. These are buffered
175 "files", and a common mistake is to \code{write} something, and
176 then \code{read} for a reply. Without a \code{flush} in
177 there, you may wait forever for the reply, because the request may
178 still be in your output buffer.
180 Now we come the major stumbling block of sockets - \code{send}
181 and \code{recv} operate on the network buffers. They do not
182 necessarily handle all the bytes you hand them (or expect from them),
183 because their major focus is handling the network buffers. In general,
184 they return when the associated network buffers have been filled
185 (\code{send}) or emptied (\code{recv}). They then tell you
186 how many bytes they handled. It is \emph{your} responsibility to call
187 them again until your message has been completely dealt with.
189 When a \code{recv} returns 0 bytes, it means the other side has
190 closed (or is in the process of closing) the connection. You will not
191 receive any more data on this connection. Ever. You may be able to
192 send data successfully; I'll talk about that some on the next page.
194 A protocol like HTTP uses a socket for only one transfer. The client
195 sends a request, the reads a reply. That's it. The socket is
196 discarded. This means that a client can detect the end of the reply by
197 receiving 0 bytes.
199 But if you plan to reuse your socket for further transfers, you need
200 to realize that \emph{there is no "EOT" (End of Transfer) on a
201 socket.} I repeat: if a socket \code{send} or
202 \code{recv} returns after handling 0 bytes, the connection has
203 been broken. If the connection has \emph{not} been broken, you may
204 wait on a \code{recv} forever, because the socket will
205 \emph{not} tell you that there's nothing more to read (for now). Now
206 if you think about that a bit, you'll come to realize a fundamental
207 truth of sockets: \emph{messages must either be fixed length} (yuck),
208 \emph{or be delimited} (shrug), \emph{or indicate how long they are}
209 (much better), \emph{or end by shutting down the connection}. The
210 choice is entirely yours, (but some ways are righter than others).
212 Assuming you don't want to end the connection, the simplest solution
213 is a fixed length message:
215 \begin{verbatim}
216 class mysocket:
217 '''demonstration class only
218 - coded for clarity, not efficiency'''
219 def __init__(self, sock=None):
220 if sock is None:
221 self.sock = socket.socket(
222 socket.AF_INET, socket.SOCK_STREAM)
223 else:
224 self.sock = sock
226 def connect(self, host, port):
227 self.sock.connect((host, port))
229 def mysend(self, msg):
230 totalsent = 0
231 while totalsent < MSGLEN:
232 sent = self.sock.send(msg[totalsent:])
233 if sent == 0:
234 raise RuntimeError, \\
235 "socket connection broken"
236 totalsent = totalsent + sent
238 def myreceive(self):
239 msg = ''
240 while len(msg) < MSGLEN:
241 chunk = self.sock.recv(MSGLEN-len(msg))
242 if chunk == '':
243 raise RuntimeError, \\
244 "socket connection broken"
245 msg = msg + chunk
246 return msg
247 \end{verbatim}
249 The sending code here is usable for almost any messaging scheme - in
250 Python you send strings, and you can use \code{len()} to
251 determine its length (even if it has embedded \code{\e 0}
252 characters). It's mostly the receiving code that gets more
253 complex. (And in C, it's not much worse, except you can't use
254 \code{strlen} if the message has embedded \code{\e 0}s.)
256 The easiest enhancement is to make the first character of the message
257 an indicator of message type, and have the type determine the
258 length. Now you have two \code{recv}s - the first to get (at
259 least) that first character so you can look up the length, and the
260 second in a loop to get the rest. If you decide to go the delimited
261 route, you'll be receiving in some arbitrary chunk size, (4096 or 8192
262 is frequently a good match for network buffer sizes), and scanning
263 what you've received for a delimiter.
265 One complication to be aware of: if your conversational protocol
266 allows multiple messages to be sent back to back (without some kind of
267 reply), and you pass \code{recv} an arbitrary chunk size, you
268 may end up reading the start of a following message. You'll need to
269 put that aside and hold onto it, until it's needed.
271 Prefixing the message with it's length (say, as 5 numeric characters)
272 gets more complex, because (believe it or not), you may not get all 5
273 characters in one \code{recv}. In playing around, you'll get
274 away with it; but in high network loads, your code will very quickly
275 break unless you use two \code{recv} loops - the first to
276 determine the length, the second to get the data part of the
277 message. Nasty. This is also when you'll discover that
278 \code{send} does not always manage to get rid of everything in
279 one pass. And despite having read this, you will eventually get bit by
282 In the interests of space, building your character, (and preserving my
283 competitive position), these enhancements are left as an exercise for
284 the reader. Lets move on to cleaning up.
286 \subsection{Binary Data}
288 It is perfectly possible to send binary data over a socket. The major
289 problem is that not all machines use the same formats for binary
290 data. For example, a Motorola chip will represent a 16 bit integer
291 with the value 1 as the two hex bytes 00 01. Intel and DEC, however,
292 are byte-reversed - that same 1 is 01 00. Socket libraries have calls
293 for converting 16 and 32 bit integers - \code{ntohl, htonl, ntohs,
294 htons} where "n" means \emph{network} and "h" means \emph{host},
295 "s" means \emph{short} and "l" means \emph{long}. Where network order
296 is host order, these do nothing, but where the machine is
297 byte-reversed, these swap the bytes around appropriately.
299 In these days of 32 bit machines, the ascii representation of binary
300 data is frequently smaller than the binary representation. That's
301 because a surprising amount of the time, all those longs have the
302 value 0, or maybe 1. The string "0" would be two bytes, while binary
303 is four. Of course, this doesn't fit well with fixed-length
304 messages. Decisions, decisions.
306 \section{Disconnecting}
308 Strictly speaking, you're supposed to use \code{shutdown} on a
309 socket before you \code{close} it. The \code{shutdown} is
310 an advisory to the socket at the other end. Depending on the argument
311 you pass it, it can mean "I'm not going to send anymore, but I'll
312 still listen", or "I'm not listening, good riddance!". Most socket
313 libraries, however, are so used to programmers neglecting to use this
314 piece of etiquette that normally a \code{close} is the same as
315 \code{shutdown(); close()}. So in most situations, an explicit
316 \code{shutdown} is not needed.
318 One way to use \code{shutdown} effectively is in an HTTP-like
319 exchange. The client sends a request and then does a
320 \code{shutdown(1)}. This tells the server "This client is done
321 sending, but can still receive." The server can detect "EOF" by a
322 receive of 0 bytes. It can assume it has the complete request. The
323 server sends a reply. If the \code{send} completes successfully
324 then, indeed, the client was still receiving.
326 Python takes the automatic shutdown a step further, and says that when a socket is garbage collected, it will automatically do a \code{close} if it's needed. But relying on this is a very bad habit. If your socket just disappears without doing a \code{close}, the socket at the other end may hang indefinitely, thinking you're just being slow. \emph{Please} \code{close} your sockets when you're done.
329 \subsection{When Sockets Die}
331 Probably the worst thing about using blocking sockets is what happens
332 when the other side comes down hard (without doing a
333 \code{close}). Your socket is likely to hang. SOCKSTREAM is a
334 reliable protocol, and it will wait a long, long time before giving up
335 on a connection. If you're using threads, the entire thread is
336 essentially dead. There's not much you can do about it. As long as you
337 aren't doing something dumb, like holding a lock while doing a
338 blocking read, the thread isn't really consuming much in the way of
339 resources. Do \emph{not} try to kill the thread - part of the reason
340 that threads are more efficient than processes is that they avoid the
341 overhead associated with the automatic recycling of resources. In
342 other words, if you do manage to kill the thread, your whole process
343 is likely to be screwed up.
345 \section{Non-blocking Sockets}
347 If you've understood the preceeding, you already know most of what you
348 need to know about the mechanics of using sockets. You'll still use
349 the same calls, in much the same ways. It's just that, if you do it
350 right, your app will be almost inside-out.
352 In Python, you use \code{socket.setblocking(0)} to make it
353 non-blocking. In C, it's more complex, (for one thing, you'll need to
354 choose between the BSD flavor \code{O_NONBLOCK} and the almost
355 indistinguishable Posix flavor \code{O_NDELAY}, which is
356 completely different from \code{TCP_NODELAY}), but it's the
357 exact same idea. You do this after creating the socket, but before
358 using it. (Actually, if you're nuts, you can switch back and forth.)
360 The major mechanical difference is that \code{send},
361 \code{recv}, \code{connect} and \code{accept} can
362 return without having done anything. You have (of course) a number of
363 choices. You can check return code and error codes and generally drive
364 yourself crazy. If you don't believe me, try it sometime. Your app
365 will grow large, buggy and suck CPU. So let's skip the brain-dead
366 solutions and do it right.
368 Use \code{select}.
370 In C, coding \code{select} is fairly complex. In Python, it's a
371 piece of cake, but it's close enough to the C version that if you
372 understand \code{select} in Python, you'll have little trouble
373 with it in C.
375 \begin{verbatim} ready_to_read, ready_to_write, in_error = \\
376 select.select(
377 potential_readers,
378 potential_writers,
379 potential_errs,
380 timeout)
381 \end{verbatim}
383 You pass \code{select} three lists: the first contains all
384 sockets that you might want to try reading; the second all the sockets
385 you might want to try writing to, and the last (normally left empty)
386 those that you want to check for errors. You should note that a
387 socket can go into more than one list. The \code{select} call is
388 blocking, but you can give it a timeout. This is generally a sensible
389 thing to do - give it a nice long timeout (say a minute) unless you
390 have good reason to do otherwise.
392 In return, you will get three lists. They have the sockets that are
393 actually readable, writable and in error. Each of these lists is a
394 subset (possbily empty) of the corresponding list you passed in. And
395 if you put a socket in more than one input list, it will only be (at
396 most) in one output list.
398 If a socket is in the output readable list, you can be
399 as-close-to-certain-as-we-ever-get-in-this-business that a
400 \code{recv} on that socket will return \emph{something}. Same
401 idea for the writable list. You'll be able to send
402 \emph{something}. Maybe not all you want to, but \emph{something} is
403 better than nothing. (Actually, any reasonably healthy socket will
404 return as writable - it just means outbound network buffer space is
405 available.)
407 If you have a "server" socket, put it in the potential_readers
408 list. If it comes out in the readable list, your \code{accept}
409 will (almost certainly) work. If you have created a new socket to
410 \code{connect} to someone else, put it in the ptoential_writers
411 list. If it shows up in the writable list, you have a decent chance
412 that it has connected.
414 One very nasty problem with \code{select}: if somewhere in those
415 input lists of sockets is one which has died a nasty death, the
416 \code{select} will fail. You then need to loop through every
417 single damn socket in all those lists and do a
418 \code{select([sock],[],[],0)} until you find the bad one. That
419 timeout of 0 means it won't take long, but it's ugly.
421 Actually, \code{select} can be handy even with blocking sockets.
422 It's one way of determining whether you will block - the socket
423 returns as readable when there's something in the buffers. However,
424 this still doesn't help with the problem of determining whether the
425 other end is done, or just busy with something else.
427 \textbf{Portability alert}: On Unix, \code{select} works both with
428 the sockets and files. Don't try this on Windows. On Windows,
429 \code{select} works with sockets only. Also note that in C, many
430 of the more advanced socket options are done differently on
431 Windows. In fact, on Windows I usually use threads (which work very,
432 very well) with my sockets. Face it, if you want any kind of
433 performance, your code will look very different on Windows than on
434 Unix. (I haven't the foggiest how you do this stuff on a Mac.)
436 \subsection{Performance}
438 There's no question that the fastest sockets code uses non-blocking
439 sockets and select to multiplex them. You can put together something
440 that will saturate a LAN connection without putting any strain on the
441 CPU. The trouble is that an app written this way can't do much of
442 anything else - it needs to be ready to shuffle bytes around at all
443 times.
445 Assuming that your app is actually supposed to do something more than
446 that, threading is the optimal solution, (and using non-blocking
447 sockets will be faster than using blocking sockets). Unfortunately,
448 threading support in Unixes varies both in API and quality. So the
449 normal Unix solution is to fork a subprocess to deal with each
450 connection. The overhead for this is significant (and don't do this on
451 Windows - the overhead of process creation is enormous there). It also
452 means that unless each subprocess is completely independent, you'll
453 need to use another form of IPC, say a pipe, or shared memory and
454 semaphores, to communicate between the parent and child processes.
456 Finally, remember that even though blocking sockets are somewhat
457 slower than non-blocking, in many cases they are the "right"
458 solution. After all, if your app is driven by the data it receives
459 over a socket, there's not much sense in complicating the logic just
460 so your app can wait on \code{select} instead of
461 \code{recv}.
463 \end{document}