[Python-checkins] cpython (2.7): Issue #13141: Demonstrate recommended style for SocketServer examples.

florent.xicluna python-checkins at python.org
Sun Oct 23 23:08:20 CEST 2011


http://hg.python.org/cpython/rev/8de472fb8cfe
changeset:   73082:8de472fb8cfe
branch:      2.7
parent:      73073:5c4781a237ef
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Sun Oct 23 23:07:22 2011 +0200
summary:
  Issue #13141: Demonstrate recommended style for SocketServer examples.

files:
  Doc/library/socketserver.rst |  48 +++++++++++++----------
  Misc/NEWS                    |   5 ++
  2 files changed, 32 insertions(+), 21 deletions(-)


diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -225,6 +225,7 @@
    desired.  If :meth:`handle_request` receives no incoming requests within the
    timeout period, the :meth:`handle_timeout` method is called.
 
+
 There are various server methods that can be overridden by subclasses of base
 server classes like :class:`TCPServer`; these methods aren't useful to external
 users of the server object.
@@ -355,7 +356,7 @@
        def handle(self):
            # self.request is the TCP socket connected to the client
            self.data = self.request.recv(1024).strip()
-           print "%s wrote:" % self.client_address[0]
+           print "{} wrote:".format(self.client_address[0])
            print self.data
            # just send back the same data, but upper-cased
            self.request.send(self.data.upper())
@@ -379,7 +380,7 @@
            # self.rfile is a file-like object created by the handler;
            # we can now use e.g. readline() instead of raw recv() calls
            self.data = self.rfile.readline().strip()
-           print "%s wrote:" % self.client_address[0]
+           print "{} wrote:".format(self.client_address[0])
            print self.data
            # Likewise, self.wfile is a file-like object used to write back
            # to the client
@@ -402,16 +403,18 @@
    # Create a socket (SOCK_STREAM means a TCP socket)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
-   # Connect to server and send data
-   sock.connect((HOST, PORT))
-   sock.send(data + "\n")
+   try:
+       # Connect to server and send data
+       sock.connect((HOST, PORT))
+       sock.send(data + "\n")
 
-   # Receive data from the server and shut down
-   received = sock.recv(1024)
-   sock.close()
+       # Receive data from the server and shut down
+       received = sock.recv(1024)
+   finally:
+       sock.close()
 
-   print "Sent:     %s" % data
-   print "Received: %s" % received
+   print "Sent:     {}".format(data)
+   print "Received: {}".format(received)
 
 
 The output of the example should look something like this:
@@ -452,7 +455,7 @@
        def handle(self):
            data = self.request[0].strip()
            socket = self.request[1]
-           print "%s wrote:" % self.client_address[0]
+           print "{} wrote:".format(self.client_address[0])
            print data
            socket.sendto(data.upper(), self.client_address)
 
@@ -477,8 +480,8 @@
    sock.sendto(data + "\n", (HOST, PORT))
    received = sock.recv(1024)
 
-   print "Sent:     %s" % data
-   print "Received: %s" % received
+   print "Sent:     {}".format(data)
+   print "Received: {}".format(received)
 
 The output of the example should look exactly like for the TCP server example.
 
@@ -499,8 +502,8 @@
 
        def handle(self):
            data = self.request.recv(1024)
-           cur_thread = threading.currentThread()
-           response = "%s: %s" % (cur_thread.getName(), data)
+           cur_thread = threading.current_thread()
+           response = "{}: {}".format(cur_thread.name, data)
            self.request.send(response)
 
    class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
@@ -509,10 +512,12 @@
    def client(ip, port, message):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((ip, port))
-       sock.send(message)
-       response = sock.recv(1024)
-       print "Received: %s" % response
-       sock.close()
+       try:
+           sock.send(message)
+           response = sock.recv(1024)
+           print "Received: {}".format(response)
+       finally:
+           sock.close()
 
    if __name__ == "__main__":
        # Port 0 means to select an arbitrary unused port
@@ -525,9 +530,9 @@
        # more thread for each request
        server_thread = threading.Thread(target=server.serve_forever)
        # Exit the server thread when the main thread terminates
-       server_thread.setDaemon(True)
+       server_thread.daemon = True
        server_thread.start()
-       print "Server loop running in thread:", server_thread.getName()
+       print "Server loop running in thread:", server_thread.name
 
        client(ip, port, "Hello World 1")
        client(ip, port, "Hello World 2")
@@ -535,6 +540,7 @@
 
        server.shutdown()
 
+
 The output of the example should look something like this::
 
    $ python ThreadedTCPServer.py
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -342,6 +342,11 @@
 - Issue #12057: Add tests for ISO 2022 codecs (iso2022_jp, iso2022_jp_2,
   iso2022_kr).
 
+Documentation
+-------------
+
+- Issue #13141: Demonstrate recommended style for SocketServer examples.
+
 
 What's New in Python 2.7.2?
 ===========================

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list