Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5)

Delta Between Two Patch Sets: Doc/library/socketserver.rst

Issue 26404: socketserver context manager
Left Patch Set: Created 3 years, 12 months ago
Right Patch Set: Created 3 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 :mod:`socketserver` --- A framework for network servers 1 :mod:`socketserver` --- A framework for network servers
2 ======================================================= 2 =======================================================
3 3
4 .. module:: socketserver 4 .. module:: socketserver
5 :synopsis: A framework for network servers. 5 :synopsis: A framework for network servers.
6 6
7 **Source code:** :source:`Lib/socketserver.py` 7 **Source code:** :source:`Lib/socketserver.py`
8 8
9 -------------- 9 --------------
10 10
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 Creating a server requires several steps. First, you must create a request 50 Creating a server requires several steps. First, you must create a request
51 handler class by subclassing the :class:`BaseRequestHandler` class and 51 handler class by subclassing the :class:`BaseRequestHandler` class and
52 overriding its :meth:`~BaseRequestHandler.handle` method; 52 overriding its :meth:`~BaseRequestHandler.handle` method;
53 this method will process incoming 53 this method will process incoming
54 requests. Second, you must instantiate one of the server classes, passing it 54 requests. Second, you must instantiate one of the server classes, passing it
55 the server's address and the request handler class. It is recommended to use 55 the server's address and the request handler class. It is recommended to use
56 the server in a :keyword:`with` statement. Then call the 56 the server in a :keyword:`with` statement. Then call the
57 :meth:`~BaseServer.handle_request` or 57 :meth:`~BaseServer.handle_request` or
58 :meth:`~BaseServer.serve_forever` method of the server object to 58 :meth:`~BaseServer.serve_forever` method of the server object to
59 process one or many requests. Finally, call :meth:`~BaseServer.server_close` 59 process one or many requests. Finally, call :meth:`~BaseServer.server_close`
60 to close the socket (unless you used the :keyword:`with` statement). 60 to close the socket (unless you used the :keyword:`with` statement).
Martin Panter 2016/04/13 04:52:57 a “with” statement (matching ‘in a “with” statemen
61 61
62 When inheriting from :class:`ThreadingMixIn` for threaded connection behavior, 62 When inheriting from :class:`ThreadingMixIn` for threaded connection behavior,
63 you should explicitly declare how you want your threads to behave on an abrupt 63 you should explicitly declare how you want your threads to behave on an abrupt
64 shutdown. The :class:`ThreadingMixIn` class defines an attribute 64 shutdown. The :class:`ThreadingMixIn` class defines an attribute
65 *daemon_threads*, which indicates whether or not the server should wait for 65 *daemon_threads*, which indicates whether or not the server should wait for
66 thread termination. You should set the flag explicitly if you would like 66 thread termination. You should set the flag explicitly if you would like
67 threads to behave autonomously; the default is :const:`False`, meaning that 67 threads to behave autonomously; the default is :const:`False`, meaning that
68 Python will not exit until all threads created by :class:`ThreadingMixIn` have 68 Python will not exit until all threads created by :class:`ThreadingMixIn` have
69 exited. 69 exited.
70 70
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 347
348 348
349 .. method:: verify_request(request, client_address) 349 .. method:: verify_request(request, client_address)
350 350
351 Must return a Boolean value; if the value is :const:`True`, the request wi ll 351 Must return a Boolean value; if the value is :const:`True`, the request wi ll
352 be processed, and if it's :const:`False`, the request will be denied. Thi s 352 be processed, and if it's :const:`False`, the request will be denied. Thi s
353 function can be overridden to implement access controls for a server. The 353 function can be overridden to implement access controls for a server. The
354 default implementation always returns :const:`True`. 354 default implementation always returns :const:`True`.
355 355
356 356
357 .. versionadded:: 3.6 357 .. versionchanged:: 3.6
berkerpeksag 2016/02/24 14:09:38 versionchanged This is an extension to an already
palaviv 2016/02/25 18:27:05 Done.
358 Support for the :term:`context manager` protocol was added. Exiting the 358 Support for the :term:`context manager` protocol was added. Exiting the
359 context manager is equivalent to calling :meth:`server_close`. 359 context manager is equivalent to calling :meth:`server_close`.
360 360
361 361
362 Request Handler Objects 362 Request Handler Objects
363 ----------------------- 363 -----------------------
364 364
365 .. class:: BaseRequestHandler 365 .. class:: BaseRequestHandler
366 366
367 This is the superclass of all request handler objects. It defines 367 This is the superclass of all request handler objects. It defines
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 sock.sendall(bytes(message, 'ascii')) 589 sock.sendall(bytes(message, 'ascii'))
590 response = str(sock.recv(1024), 'ascii') 590 response = str(sock.recv(1024), 'ascii')
591 print("Received: {}".format(response)) 591 print("Received: {}".format(response))
592 finally: 592 finally:
593 sock.close() 593 sock.close()
594 594
595 if __name__ == "__main__": 595 if __name__ == "__main__":
596 # Port 0 means to select an arbitrary unused port 596 # Port 0 means to select an arbitrary unused port
597 HOST, PORT = "localhost", 0 597 HOST, PORT = "localhost", 0
598 598
599 with ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) as server : 599 with ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) as server :
Martin Panter 2016/04/13 04:52:57 Split this into two lines to limit the width
600 ip, port = server.server_address 600 ip, port = server.server_address
601 601
602 # Start a thread with the server -- that thread will then start one 602 # Start a thread with the server -- that thread will then start one
603 # more thread for each request 603 # more thread for each request
604 server_thread = threading.Thread(target=server.serve_forever) 604 server_thread = threading.Thread(target=server.serve_forever)
605 # Exit the server thread when the main thread terminates 605 # Exit the server thread when the main thread terminates
606 server_thread.daemon = True 606 server_thread.daemon = True
607 server_thread.start() 607 server_thread.start()
608 print("Server loop running in thread:", server_thread.name) 608 print("Server loop running in thread:", server_thread.name)
609 609
610 client(ip, port, "Hello World 1") 610 client(ip, port, "Hello World 1")
611 client(ip, port, "Hello World 2") 611 client(ip, port, "Hello World 2")
612 client(ip, port, "Hello World 3") 612 client(ip, port, "Hello World 3")
613 613
614 server.shutdown() 614 server.shutdown()
615 615
616 616
617 The output of the example should look something like this:: 617 The output of the example should look something like this::
618 618
619 $ python ThreadedTCPServer.py 619 $ python ThreadedTCPServer.py
620 Server loop running in thread: Thread-1 620 Server loop running in thread: Thread-1
621 Received: Thread-2: Hello World 1 621 Received: Thread-2: Hello World 1
622 Received: Thread-3: Hello World 2 622 Received: Thread-3: Hello World 2
623 Received: Thread-4: Hello World 3 623 Received: Thread-4: Hello World 3
624 624
625 625
626 The :class:`ForkingMixIn` class is used in the same way, except that the server 626 The :class:`ForkingMixIn` class is used in the same way, except that the server
627 will spawn a new process for each request. 627 will spawn a new process for each request.
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+