[Python-checkins] cpython (merge default -> default): Merge heads

florent.xicluna python-checkins at python.org
Sun Oct 23 22:48:12 CEST 2011


http://hg.python.org/cpython/rev/a4f7815e1691
changeset:   73081:a4f7815e1691
parent:      73078:efd2afd7cf16
parent:      73080:7f8d22a7bec3
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Sun Oct 23 22:47:56 2011 +0200
summary:
  Merge heads

files:
  Doc/library/socketserver.rst |  64 ++++++++++++-----------
  Misc/NEWS                    |   2 +
  2 files changed, 36 insertions(+), 30 deletions(-)


diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -361,7 +361,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())
@@ -385,7 +385,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
@@ -408,16 +408,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(bytes(data + "\n","utf8"))
+   try:
+       # Connect to server and send data
+       sock.connect((HOST, PORT))
+       sock.send(bytes(data + "\n", "utf-8"))
 
-   # Receive data from the server and shut down
-   received = sock.recv(1024)
-   sock.close()
+       # Receive data from the server and shut down
+       received = str(sock.recv(1024), "utf-8")
+   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:
@@ -434,10 +436,10 @@
 
    $ python TCPClient.py hello world with TCP
    Sent:     hello world with TCP
-   Received: b'HELLO WORLD WITH TCP'
+   Received: HELLO WORLD WITH TCP
    $ python TCPClient.py python is nice
    Sent:     python is nice
-   Received: b'PYTHON IS NICE'
+   Received: PYTHON IS NICE
 
 
 :class:`socketserver.UDPServer` Example
@@ -458,7 +460,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)
 
@@ -480,11 +482,11 @@
 
    # As you can see, there is no connect() call; UDP has no connections.
    # Instead, data is directly sent to the recipient via sendto().
-   sock.sendto(bytes(data + "\n","utf8"), (HOST, PORT))
-   received = sock.recv(1024)
+   sock.sendto(bytes(data + "\n", "utf-8"), (HOST, PORT))
+   received = str(sock.recv(1024), "utf-8")
 
-   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.
 
@@ -504,9 +506,9 @@
    class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
 
        def handle(self):
-           data = self.request.recv(1024)
+           data = str(self.request.recv(1024), 'ascii')
            cur_thread = threading.current_thread()
-           response = bytes("%s: %s" % (cur_thread.getName(), data),'ascii')
+           response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
            self.request.send(response)
 
    class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@@ -515,10 +517,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(bytes(message, 'ascii'))
+           response = str(sock.recv(1024), 'ascii')
+           print("Received: {}".format(response))
+       finally:
+           sock.close()
 
    if __name__ == "__main__":
        # Port 0 means to select an arbitrary unused port
@@ -531,13 +535,13 @@
        # 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.name)
 
-       client(ip, port, b"Hello World 1")
-       client(ip, port, b"Hello World 2")
-       client(ip, port, b"Hello World 3")
+       client(ip, port, "Hello World 1")
+       client(ip, port, "Hello World 2")
+       client(ip, port, "Hello World 3")
 
        server.shutdown()
 
@@ -546,9 +550,9 @@
 
    $ python ThreadedTCPServer.py
    Server loop running in thread: Thread-1
-   Received: b"Thread-2: b'Hello World 1'"
-   Received: b"Thread-3: b'Hello World 2'"
-   Received: b"Thread-4: b'Hello World 3'"
+   Received: Thread-2: Hello World 1
+   Received: Thread-3: Hello World 2
+   Received: Thread-4: Hello World 3
 
 
 The :class:`ForkingMixIn` class is used in the same way, except that the server
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1678,6 +1678,8 @@
 Documentation
 -------------
 
+- Issue #13141: Demonstrate recommended style for socketserver examples.
+
 - Issue #11818: Fix tempfile examples for Python 3.
 
 

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


More information about the Python-checkins mailing list