Mercurial > cpython-lru_cache-weakref
annotate Doc/library/socketserver.rst @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 3889d9528601 |
| children | 85aa706f7126 da6871b9d884 |
| rev | line source |
|---|---|
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
1 |
|
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
2 :mod:`SocketServer` --- A framework for network servers |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 ======================================================= |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 |
|
46939
4ac9cc1d7798
Fix-up docs for socketserver and queue renaming.
Georg Brandl <georg@python.org>
parents:
46930
diff
changeset
|
5 .. module:: SocketServer |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 :synopsis: A framework for network servers. |
|
46939
4ac9cc1d7798
Fix-up docs for socketserver and queue renaming.
Georg Brandl <georg@python.org>
parents:
46930
diff
changeset
|
7 |
|
4ac9cc1d7798
Fix-up docs for socketserver and queue renaming.
Georg Brandl <georg@python.org>
parents:
46930
diff
changeset
|
8 .. note:: |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
9 |
|
50522
ee9ced92843a
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
10 The :mod:`SocketServer` module has been renamed to :mod:`socketserver` in |
|
ee9ced92843a
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
11 Python 3.0. The :term:`2to3` tool will automatically adapt imports when |
|
ee9ced92843a
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
12 converting your sources to 3.0. |
|
46930
dafde2eee3d0
Added stub entry in the documentation mentioning SocketServer
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46929
diff
changeset
|
13 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
15 The :mod:`SocketServer` module simplifies the task of writing network servers. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 There are four basic server classes: :class:`TCPServer` uses the Internet TCP |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 protocol, which provides for continuous streams of data between the client and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 server. :class:`UDPServer` uses datagrams, which are discrete packets of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
20 information that may arrive out of order or be lost while in transit. The more |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 infrequently used :class:`UnixStreamServer` and :class:`UnixDatagramServer` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 classes are similar, but use Unix domain sockets; they're not available on |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
23 non-Unix platforms. For more details on network programming, consult a book |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
24 such as |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 W. Richard Steven's UNIX Network Programming or Ralph Davis's Win32 Network |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 Programming. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 These four classes process requests :dfn:`synchronously`; each request must be |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 completed before the next request can be started. This isn't suitable if each |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 request takes a long time to complete, because it requires a lot of computation, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 or because it returns a lot of data which the client is slow to process. The |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 solution is to create a separate process or thread to handle each request; the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
33 :class:`ForkingMixIn` and :class:`ThreadingMixIn` mix-in classes can be used to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 support asynchronous behaviour. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
35 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
36 Creating a server requires several steps. First, you must create a request |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 handler class by subclassing the :class:`BaseRequestHandler` class and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 overriding its :meth:`handle` method; this method will process incoming |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 requests. Second, you must instantiate one of the server classes, passing it |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 the server's address and the request handler class. Finally, call the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 :meth:`handle_request` or :meth:`serve_forever` method of the server object to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 process one or many requests. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
44 When inheriting from :class:`ThreadingMixIn` for threaded connection behavior, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
45 you should explicitly declare how you want your threads to behave on an abrupt |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
46 shutdown. The :class:`ThreadingMixIn` class defines an attribute |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 *daemon_threads*, which indicates whether or not the server should wait for |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
48 thread termination. You should set the flag explicitly if you would like threads |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 to behave autonomously; the default is :const:`False`, meaning that Python will |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 not exit until all threads created by :class:`ThreadingMixIn` have exited. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 Server classes have the same external methods and attributes, no matter what |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
53 network protocol they use. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 Server Creation Notes |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 --------------------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
58 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 There are five classes in an inheritance diagram, four of which represent |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
60 synchronous servers of four types:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
61 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 +------------+ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
63 | BaseServer | |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
64 +------------+ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
65 | |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
66 v |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
67 +-----------+ +------------------+ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
68 | TCPServer |------->| UnixStreamServer | |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
69 +-----------+ +------------------+ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
70 | |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
71 v |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 +-----------+ +--------------------+ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
73 | UDPServer |------->| UnixDatagramServer | |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
74 +-----------+ +--------------------+ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
75 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 Note that :class:`UnixDatagramServer` derives from :class:`UDPServer`, not from |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
77 :class:`UnixStreamServer` --- the only difference between an IP and a Unix |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 stream server is the address family, which is simply repeated in both Unix |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 server classes. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
80 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
81 Forking and threading versions of each type of server can be created using the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
82 :class:`ForkingMixIn` and :class:`ThreadingMixIn` mix-in classes. For instance, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
83 a threading UDP server class is created as follows:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
84 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
86 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 The mix-in class must come first, since it overrides a method defined in |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
88 :class:`UDPServer`. Setting the various member variables also changes the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 behavior of the underlying server mechanism. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 To implement a service, you must derive a class from :class:`BaseRequestHandler` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
92 and redefine its :meth:`handle` method. You can then run various versions of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
93 the service by combining one of the server classes with your request handler |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
94 class. The request handler class must be different for datagram or stream |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
95 services. This can be hidden by using the handler subclasses |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
96 :class:`StreamRequestHandler` or :class:`DatagramRequestHandler`. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
97 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
98 Of course, you still have to use your head! For instance, it makes no sense to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
99 use a forking server if the service contains state in memory that can be |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
100 modified by different requests, since the modifications in the child process |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
101 would never reach the initial state kept in the parent process and passed to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
102 each child. In this case, you can use a threading server, but you will probably |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
103 have to use locks to protect the integrity of the shared data. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
104 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 On the other hand, if you are building an HTTP server where all data is stored |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 externally (for instance, in the file system), a synchronous class will |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
107 essentially render the service "deaf" while one request is being handled -- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
108 which may be for a very long time if a client is slow to receive all the data it |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
109 has requested. Here a threading or forking server is appropriate. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 In some cases, it may be appropriate to process part of a request synchronously, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 but to finish processing in a forked child depending on the request data. This |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 can be implemented by using a synchronous server and doing an explicit fork in |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 the request handler class :meth:`handle` method. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 Another approach to handling multiple simultaneous requests in an environment |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 that supports neither threads nor :func:`fork` (or where these are too expensive |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 or inappropriate for the service) is to maintain an explicit table of partially |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 finished requests and to use :func:`select` to decide which request to work on |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 next (or whether to handle a new incoming request). This is particularly |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 important for stream services where each client can potentially be connected for |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
122 a long time (if threads or subprocesses cannot be used). See :mod:`asyncore` for |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
123 another way to manage this. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 |
|
44396
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
125 .. XXX should data and methods be intermingled, or separate? |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
126 how should the distinction between class and instance variables be drawn? |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
127 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
128 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
129 Server Objects |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
130 -------------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
132 .. class:: BaseServer |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
134 This is the superclass of all Server objects in the module. It defines the |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
135 interface, given below, but does not implement most of the methods, which is |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
136 done in subclasses. |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
137 |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
138 |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
139 .. method:: BaseServer.fileno() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
140 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 Return an integer file descriptor for the socket on which the server is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
142 listening. This function is most commonly passed to :func:`select.select`, to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
143 allow monitoring multiple servers in the same process. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
144 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
145 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
146 .. method:: BaseServer.handle_request() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
148 Process a single request. This function calls the following methods in |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
149 order: :meth:`get_request`, :meth:`verify_request`, and |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
150 :meth:`process_request`. If the user-provided :meth:`handle` method of the |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
151 handler class raises an exception, the server's :meth:`handle_error` method |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
152 will be called. If no request is received within :attr:`self.timeout` |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
153 seconds, :meth:`handle_timeout` will be called and :meth:`handle_request` |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
154 will return. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
155 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
156 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
157 .. method:: BaseServer.serve_forever(poll_interval=0.5) |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
158 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
159 Handle requests until an explicit :meth:`shutdown` request. Polls for |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
160 shutdown every *poll_interval* seconds. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
161 |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
162 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
163 .. method:: BaseServer.shutdown() |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
164 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
165 Tells the :meth:`serve_forever` loop to stop and waits until it does. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
166 |
|
47830
8821b405e0e6
Add a version tag for shutdown().
Georg Brandl <georg@python.org>
parents:
47241
diff
changeset
|
167 .. versionadded:: 2.6 |
|
8821b405e0e6
Add a version tag for shutdown().
Georg Brandl <georg@python.org>
parents:
47241
diff
changeset
|
168 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
169 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
170 .. attribute:: BaseServer.address_family |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
171 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
172 The family of protocols to which the server's socket belongs. |
|
46878
9c765f4ed512
#2741: clarification of value range for address_family.
Georg Brandl <georg@python.org>
parents:
45702
diff
changeset
|
173 Common examples are :const:`socket.AF_INET` and :const:`socket.AF_UNIX`. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
174 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
175 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
176 .. attribute:: BaseServer.RequestHandlerClass |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
177 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
178 The user-provided request handler class; an instance of this class is created |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
179 for each request. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
180 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
181 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
182 .. attribute:: BaseServer.server_address |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
183 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
184 The address on which the server is listening. The format of addresses varies |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 depending on the protocol family; see the documentation for the socket module |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 for details. For Internet protocols, this is a tuple containing a string giving |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
187 the address, and an integer port number: ``('127.0.0.1', 80)``, for example. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
188 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
189 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
190 .. attribute:: BaseServer.socket |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
191 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
192 The socket object on which the server will listen for incoming requests. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
193 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
194 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
195 The server classes support the following class variables: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
196 |
|
44396
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
197 .. XXX should class variables be covered before instance variables, or vice versa? |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
198 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
199 .. attribute:: BaseServer.allow_reuse_address |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
200 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
201 Whether the server will allow the reuse of an address. This defaults to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
202 :const:`False`, and can be set in subclasses to change the policy. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
203 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
205 .. attribute:: BaseServer.request_queue_size |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
206 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
207 The size of the request queue. If it takes a long time to process a single |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
208 request, any requests that arrive while the server is busy are placed into a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 queue, up to :attr:`request_queue_size` requests. Once the queue is full, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
210 further requests from clients will get a "Connection denied" error. The default |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 value is usually 5, but this can be overridden by subclasses. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
212 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
214 .. attribute:: BaseServer.socket_type |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 The type of socket used by the server; :const:`socket.SOCK_STREAM` and |
|
46878
9c765f4ed512
#2741: clarification of value range for address_family.
Georg Brandl <georg@python.org>
parents:
45702
diff
changeset
|
217 :const:`socket.SOCK_DGRAM` are two common values. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
219 |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
220 .. attribute:: BaseServer.timeout |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
221 |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
222 Timeout duration, measured in seconds, or :const:`None` if no timeout is |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
223 desired. If :meth:`handle_request` receives no incoming requests within the |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44768
diff
changeset
|
224 timeout period, the :meth:`handle_timeout` method is called. |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
225 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
226 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
227 There are various server methods that can be overridden by subclasses of base |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
228 server classes like :class:`TCPServer`; these methods aren't useful to external |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
229 users of the server object. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
230 |
|
44396
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
231 .. XXX should the default implementations of these be documented, or should |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
232 it be assumed that the user will look at SocketServer.py? |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
233 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
234 .. method:: BaseServer.finish_request() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
235 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
236 Actually processes the request by instantiating :attr:`RequestHandlerClass` and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
237 calling its :meth:`handle` method. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
238 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
239 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
240 .. method:: BaseServer.get_request() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
241 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
242 Must accept a request from the socket, and return a 2-tuple containing the *new* |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
243 socket object to be used to communicate with the client, and the client's |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
244 address. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
245 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
246 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
247 .. method:: BaseServer.handle_error(request, client_address) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
248 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
249 This function is called if the :attr:`RequestHandlerClass`'s :meth:`handle` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
250 method raises an exception. The default action is to print the traceback to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
251 standard output and continue handling further requests. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
252 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
253 |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
254 .. method:: BaseServer.handle_timeout() |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
255 |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
256 This function is called when the :attr:`timeout` attribute has been set to a |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
257 value other than :const:`None` and the timeout period has passed with no |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
258 requests being received. The default action for forking servers is |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
259 to collect the status of any child processes that have exited, while |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
44396
diff
changeset
|
260 in threading servers this method does nothing. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
261 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
262 |
|
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
263 .. method:: BaseServer.process_request(request, client_address) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
264 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
265 Calls :meth:`finish_request` to create an instance of the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
266 :attr:`RequestHandlerClass`. If desired, this function can create a new process |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
267 or thread to handle the request; the :class:`ForkingMixIn` and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
268 :class:`ThreadingMixIn` classes do this. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
269 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
270 |
|
44396
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
271 .. Is there any point in documenting the following two functions? |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
272 What would the purpose of overriding them be: initializing server |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42604
diff
changeset
|
273 instance variables, adding new network families? |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
275 .. method:: BaseServer.server_activate() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 Called by the server's constructor to activate the server. The default behavior |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
278 just :meth:`listen`\ s to the server's socket. May be overridden. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
279 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
280 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
281 .. method:: BaseServer.server_bind() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
282 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 Called by the server's constructor to bind the socket to the desired address. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
284 May be overridden. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
285 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
287 .. method:: BaseServer.verify_request(request, client_address) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
289 Must return a Boolean value; if the value is :const:`True`, the request will be |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
290 processed, and if it's :const:`False`, the request will be denied. This function |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
291 can be overridden to implement access controls for a server. The default |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 implementation always returns :const:`True`. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 RequestHandler Objects |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
296 ---------------------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
297 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
298 The request handler class must define a new :meth:`handle` method, and can |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
299 override any of the following methods. A new instance is created for each |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
300 request. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
301 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
302 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
303 .. method:: RequestHandler.finish() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
304 |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
305 Called after the :meth:`handle` method to perform any clean-up actions |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
306 required. The default implementation does nothing. If :meth:`setup` or |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
307 :meth:`handle` raise an exception, this function will not be called. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
308 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
309 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
310 .. method:: RequestHandler.handle() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
311 |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
312 This function must do all the work required to service a request. The |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
313 default implementation does nothing. Several instance attributes are |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
314 available to it; the request is available as :attr:`self.request`; the client |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
315 address as :attr:`self.client_address`; and the server instance as |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
316 :attr:`self.server`, in case it needs access to per-server information. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
317 |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
318 The type of :attr:`self.request` is different for datagram or stream |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
319 services. For stream services, :attr:`self.request` is a socket object; for |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
320 datagram services, :attr:`self.request` is a pair of string and socket. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
321 However, this can be hidden by using the request handler subclasses |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
322 :class:`StreamRequestHandler` or :class:`DatagramRequestHandler`, which |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
323 override the :meth:`setup` and :meth:`finish` methods, and provide |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
324 :attr:`self.rfile` and :attr:`self.wfile` attributes. :attr:`self.rfile` and |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
325 :attr:`self.wfile` can be read or written, respectively, to get the request |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
326 data or return data to the client. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
327 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
328 |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51809
diff
changeset
|
329 .. method:: RequestHandler.setup() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
330 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
331 Called before the :meth:`handle` method to perform any initialization actions |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
332 required. The default implementation does nothing. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
333 |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
334 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
335 Examples |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
336 -------- |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
337 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
338 :class:`SocketServer.TCPServer` Example |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
339 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
340 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
341 This is the server side:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
342 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
343 import SocketServer |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
344 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
345 class MyTCPHandler(SocketServer.BaseRequestHandler): |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
346 """ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
347 The RequestHandler class for our server. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
348 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
349 It is instantiated once per connection to the server, and must |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
350 override the handle() method to implement communication to the |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
351 client. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
352 """ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
353 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
354 def handle(self): |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
355 # self.request is the TCP socket connected to the client |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
356 self.data = self.request.recv(1024).strip() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
357 print "%s wrote:" % self.client_address[0] |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
358 print self.data |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
359 # just send back the same data, but upper-cased |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
360 self.request.send(self.data.upper()) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
361 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
362 if __name__ == "__main__": |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
363 HOST, PORT = "localhost", 9999 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
364 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
365 # Create the server, binding to localhost on port 9999 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
366 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
367 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
368 # Activate the server; this will keep running until you |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
369 # interrupt the program with Ctrl-C |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
370 server.serve_forever() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
371 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
372 An alternative request handler class that makes use of streams (file-like |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
373 objects that simplify communication by providing the standard file interface):: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
374 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
375 class MyTCPHandler(SocketServer.StreamRequestHandler): |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
376 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
377 def handle(self): |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
378 # self.rfile is a file-like object created by the handler; |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
379 # we can now use e.g. readline() instead of raw recv() calls |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
380 self.data = self.rfile.readline().strip() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
381 print "%s wrote:" % self.client_address[0] |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
382 print self.data |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
383 # Likewise, self.wfile is a file-like object used to write back |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
384 # to the client |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
385 self.wfile.write(self.data.upper()) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
386 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
387 The difference is that the ``readline()`` call in the second handler will call |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
388 ``recv()`` multiple times until it encounters a newline character, while the |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
389 single ``recv()`` call in the first handler will just return what has been sent |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
390 from the client in one ``send()`` call. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
391 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
392 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
393 This is the client side:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
394 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
395 import socket |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
396 import sys |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
397 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
398 HOST, PORT = "localhost", 9999 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
399 data = " ".join(sys.argv[1:]) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
400 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
401 # Create a socket (SOCK_STREAM means a TCP socket) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
402 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
403 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
404 # Connect to server and send data |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
405 sock.connect((HOST, PORT)) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
406 sock.send(data + "\n") |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
407 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
408 # Receive data from the server and shut down |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
409 received = sock.recv(1024) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
410 sock.close() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
411 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
412 print "Sent: %s" % data |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
413 print "Received: %s" % received |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
414 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
415 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
416 The output of the example should look something like this: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
417 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
418 Server:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
419 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
420 $ python TCPServer.py |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
421 127.0.0.1 wrote: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
422 hello world with TCP |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
423 127.0.0.1 wrote: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
424 python is nice |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
425 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
426 Client:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
427 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
428 $ python TCPClient.py hello world with TCP |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
429 Sent: hello world with TCP |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
430 Received: HELLO WORLD WITH TCP |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
431 $ python TCPClient.py python is nice |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
432 Sent: python is nice |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
433 Received: PYTHON IS NICE |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
434 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
435 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
436 :class:`SocketServer.UDPServer` Example |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
437 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
438 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
439 This is the server side:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
440 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
441 import SocketServer |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
442 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
443 class MyUDPHandler(SocketServer.BaseRequestHandler): |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
444 """ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
445 This class works similar to the TCP handler class, except that |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
446 self.request consists of a pair of data and client socket, and since |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
447 there is no connection the client address must be given explicitly |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
448 when sending data back via sendto(). |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
449 """ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
450 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
451 def handle(self): |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
452 data = self.request[0].strip() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
453 socket = self.request[1] |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
454 print "%s wrote:" % self.client_address[0] |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
455 print data |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
456 socket.sendto(data.upper(), self.client_address) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
457 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
458 if __name__ == "__main__": |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
459 HOST, PORT = "localhost", 9999 |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47830
diff
changeset
|
460 server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler) |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
461 server.serve_forever() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
462 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
463 This is the client side:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
464 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
465 import socket |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
466 import sys |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
467 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
468 HOST, PORT = "localhost" |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
469 data = " ".join(sys.argv[1:]) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
470 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
471 # SOCK_DGRAM is the socket type to use for UDP sockets |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
472 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
473 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
474 # As you can see, there is no connect() call; UDP has no connections. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
475 # Instead, data is directly sent to the recipient via sendto(). |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
476 sock.sendto(data + "\n", (HOST, PORT)) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
477 received = sock.recv(1024) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
478 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
479 print "Sent: %s" % data |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
480 print "Received: %s" % received |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
481 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
482 The output of the example should look exactly like for the TCP server example. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
483 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
484 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
485 Asynchronous Mixins |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
486 ~~~~~~~~~~~~~~~~~~~ |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
487 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
488 To build asynchronous handlers, use the :class:`ThreadingMixIn` and |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
489 :class:`ForkingMixIn` classes. |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
490 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
491 An example for the :class:`ThreadingMixIn` class:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
492 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
493 import socket |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
494 import threading |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
495 import SocketServer |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
496 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
497 class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
498 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
499 def handle(self): |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
500 data = self.request.recv(1024) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
501 cur_thread = threading.currentThread() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
502 response = "%s: %s" % (cur_thread.getName(), data) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
503 self.request.send(response) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
504 |
|
47241
ba1fa511b72d
socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents:
47152
diff
changeset
|
505 class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
506 pass |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
507 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
508 def client(ip, port, message): |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
509 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
510 sock.connect((ip, port)) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
511 sock.send(message) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
512 response = sock.recv(1024) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
513 print "Received: %s" % response |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
514 sock.close() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
515 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
516 if __name__ == "__main__": |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
517 # Port 0 means to select an arbitrary unused port |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
518 HOST, PORT = "localhost", 0 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
519 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
520 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
521 ip, port = server.server_address |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
522 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
523 # Start a thread with the server -- that thread will then start one |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
524 # more thread for each request |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
525 server_thread = threading.Thread(target=server.serve_forever) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
526 # Exit the server thread when the main thread terminates |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
527 server_thread.setDaemon(True) |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
528 server_thread.start() |
|
51809
a366688a8f0d
Merged revisions 68582,68718,68720-68721,68724-68727,68859,68973,69288-69289,69293,69295,69297-69301,69409,69414,69570,69573,69576,69728-69730,69769,69776,69803-69805,69840,69896 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50522
diff
changeset
|
529 print "Server loop running in thread:", server_thread.getName() |
|
47152
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
530 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
531 client(ip, port, "Hello World 1") |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
532 client(ip, port, "Hello World 2") |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
533 client(ip, port, "Hello World 3") |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
534 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
535 server.shutdown() |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
536 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
537 The output of the example should look something like this:: |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
538 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
539 $ python ThreadedTCPServer.py |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
540 Server loop running in thread: Thread-1 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
541 Received: Thread-2: Hello World 1 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
542 Received: Thread-3: Hello World 2 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
543 Received: Thread-4: Hello World 3 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
544 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
545 |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
546 The :class:`ForkingMixIn` class is used in the same way, except that the server |
|
9f354001ea25
GHOP #180 by Michael Schneider: add examples to the socketserver documentation.
Georg Brandl <georg@python.org>
parents:
46939
diff
changeset
|
547 will spawn a new process for each request. |
