[Python-3000-checkins] r67168 - in python/branches/py3k: Demo/sockets/udpecho.py Demo/sockets/unixclient.py Doc/library/socketserver.rst Misc/ACKS Misc/NEWS

benjamin.peterson python-3000-checkins at python.org
Sat Nov 8 18:24:34 CET 2008


Author: benjamin.peterson
Date: Sat Nov  8 18:24:34 2008
New Revision: 67168

Log:
fix the socketserver demo code for py3k

#4275 Thanks to Don MacMillen


Modified:
   python/branches/py3k/Demo/sockets/udpecho.py
   python/branches/py3k/Demo/sockets/unixclient.py
   python/branches/py3k/Doc/library/socketserver.rst
   python/branches/py3k/Misc/ACKS
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Demo/sockets/udpecho.py
==============================================================================
--- python/branches/py3k/Demo/sockets/udpecho.py	(original)
+++ python/branches/py3k/Demo/sockets/udpecho.py	Sat Nov  8 18:24:34 2008
@@ -56,7 +56,8 @@
         line = sys.stdin.readline()
         if not line:
             break
-        s.sendto(line, addr)
+        print('addr = ', addr)
+        s.sendto(bytes(line, 'ascii'), addr)
         data, fromaddr = s.recvfrom(BUFSIZE)
         print('client received %r from %r' % (data, fromaddr))
 

Modified: python/branches/py3k/Demo/sockets/unixclient.py
==============================================================================
--- python/branches/py3k/Demo/sockets/unixclient.py	(original)
+++ python/branches/py3k/Demo/sockets/unixclient.py	Sat Nov  8 18:24:34 2008
@@ -6,7 +6,7 @@
 FILE = 'unix-socket'
 s = socket(AF_UNIX, SOCK_STREAM)
 s.connect(FILE)
-s.send('Hello, world')
+s.send(b'Hello, world')
 data = s.recv(1024)
 s.close()
 print('Received', repr(data))

Modified: python/branches/py3k/Doc/library/socketserver.rst
==============================================================================
--- python/branches/py3k/Doc/library/socketserver.rst	(original)
+++ python/branches/py3k/Doc/library/socketserver.rst	Sat Nov  8 18:24:34 2008
@@ -336,8 +336,8 @@
        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 self.data
+           print("%s wrote:" % self.client_address[0])
+           print(self.data)
            # just send back the same data, but upper-cased
            self.request.send(self.data.upper())
 
@@ -360,8 +360,8 @@
            # 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 self.data
+           print("%s wrote:" % self.client_address[0])
+           print(self.data)
            # Likewise, self.wfile is a file-like object used to write back
            # to the client
            self.wfile.write(self.data.upper())
@@ -385,14 +385,14 @@
 
    # Connect to server and send data
    sock.connect((HOST, PORT))
-   sock.send(data + "\n")
+   sock.send(bytes(data + "\n","utf8"))
 
    # Receive data from the server and shut down
    received = sock.recv(1024)
    sock.close()
 
-   print "Sent:     %s" % data
-   print "Received: %s" % received
+   print("Sent:     %s" % data)
+   print("Received: %s" % received)
 
 
 The output of the example should look something like this:
@@ -401,18 +401,18 @@
 
    $ python TCPServer.py
    127.0.0.1 wrote:
-   hello world with TCP
+   b'hello world with TCP'
    127.0.0.1 wrote:
-   python is nice
+   b'python is nice'
 
 Client::
 
    $ python TCPClient.py hello world with TCP
    Sent:     hello world with TCP
-   Received: HELLO WORLD WITH TCP
+   Received: b'HELLO WORLD WITH TCP'
    $ python TCPClient.py python is nice
    Sent:     python is nice
-   Received: PYTHON IS NICE
+   Received: b'PYTHON IS NICE'
 
 
 :class:`socketserver.UDPServer` Example
@@ -433,13 +433,13 @@
        def handle(self):
            data = self.request[0].strip()
            socket = self.request[1]
-           print "%s wrote:" % self.client_address[0]
-           print data
+           print("%s wrote:" % self.client_address[0])
+           print(data)
            socket.sendto(data.upper(), self.client_address)
 
    if __name__ == "__main__":
       HOST, PORT = "localhost", 9999
-      server = socketserver.UDPServer((HOST, PORT), BaseUDPRequestHandler)
+      server = socketserver.UDPServer((HOST, PORT), MyUDPHandler)
       server.serve_forever()
 
 This is the client side::
@@ -447,7 +447,7 @@
    import socket
    import sys
 
-   HOST, PORT = "localhost"
+   HOST, PORT = "localhost", 9999
    data = " ".join(sys.argv[1:])
 
    # SOCK_DGRAM is the socket type to use for UDP sockets
@@ -455,11 +455,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(data + "\n", (HOST, PORT))
+   sock.sendto(bytes(data + "\n","utf8"), (HOST, PORT))
    received = sock.recv(1024)
 
-   print "Sent:     %s" % data
-   print "Received: %s" % received
+   print("Sent:     %s" % data)
+   print("Received: %s" % received)
 
 The output of the example should look exactly like for the TCP server example.
 
@@ -481,7 +481,7 @@
        def handle(self):
            data = self.request.recv(1024)
            cur_thread = threading.current_thread()
-           response = "%s: %s" % (cur_thread.get_name(), data)
+           response = bytes("%s: %s" % (cur_thread.getName(), data),'ascii')
            self.request.send(response)
 
    class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@@ -492,7 +492,7 @@
        sock.connect((ip, port))
        sock.send(message)
        response = sock.recv(1024)
-       print "Received: %s" % response
+       print("Received: %s" % response)
        sock.close()
 
    if __name__ == "__main__":
@@ -506,23 +506,24 @@
        # more thread for each request
        server_thread = threading.Thread(target=server.serve_forever)
        # Exit the server thread when the main thread terminates
-       server_thread.set_daemon(True)
+       server_thread.setDaemon(True)
        server_thread.start()
-       print "Server loop running in thread:", t.get_name()
+       print("Server loop running in thread:", server_thread.getName())
 
-       client(ip, port, "Hello World 1")
-       client(ip, port, "Hello World 2")
-       client(ip, port, "Hello World 3")
+       client(ip, port, b"Hello World 1")
+       client(ip, port, b"Hello World 2")
+       client(ip, port, b"Hello World 3")
 
        server.shutdown()
 
+
 The output of the example should look something like this::
 
    $ python ThreadedTCPServer.py
    Server loop running in thread: Thread-1
-   Received: Thread-2: Hello World 1
-   Received: Thread-3: Hello World 2
-   Received: Thread-4: Hello World 3
+   Received: b"Thread-2: b'Hello World 1'"
+   Received: b"Thread-3: b'Hello World 2'"
+   Received: b"Thread-4: b'Hello World 3'"
 
 
 The :class:`ForkingMixIn` class is used in the same way, except that the server

Modified: python/branches/py3k/Misc/ACKS
==============================================================================
--- python/branches/py3k/Misc/ACKS	(original)
+++ python/branches/py3k/Misc/ACKS	Sat Nov  8 18:24:34 2008
@@ -435,6 +435,7 @@
 Andrew I MacIntyre
 Tim MacKenzie
 Nick Maclaren
+Don MacMillen
 Steve Majewski
 Grzegorz Makarewicz
 Ken Manheimer

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Nov  8 18:24:34 2008
@@ -23,6 +23,11 @@
 
 - Issue #1656675: Register a drop handler for .py* files on Windows.
 
+Tools/Demos
+-----------
+
+- Demos of the socketserver module now work with Python 3.
+
 
 What's New in Python 3.0 release candidate 2
 ============================================


More information about the Python-3000-checkins mailing list