diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-11-08 17:24:34 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-11-08 17:24:34 (GMT) |
commit | 06fd5f8cc854c79b5a4b113d7ef9ab01227d94cf (patch) | |
tree | 36c5625b8dcb782c92f6e67fc566659c3dda9fce /Doc/library | |
parent | ef6a19e3eab6dca9ef7eba92b636a4c2e88979c4 (diff) | |
download | cpython-06fd5f8cc854c79b5a4b113d7ef9ab01227d94cf.zip cpython-06fd5f8cc854c79b5a4b113d7ef9ab01227d94cf.tar.gz cpython-06fd5f8cc854c79b5a4b113d7ef9ab01227d94cf.tar.bz2 |
fix the socketserver demo code for py3k
#4275 Thanks to Don MacMillen
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/socketserver.rst | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst index 221de2e..d827b9e 100644 --- a/Doc/library/socketserver.rst +++ b/Doc/library/socketserver.rst @@ -336,8 +336,8 @@ This is the server side:: 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 @@ objects that simplify communication by providing the standard file interface):: # 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 @@ This is the client side:: # 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 @@ Server:: $ 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 @@ This is the server side:: 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 @@ This is the client side:: 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 @@ This is the client side:: # 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 @@ An example for the :class:`ThreadingMixIn` class:: 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 @@ An example for the :class:`ThreadingMixIn` class:: 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 @@ An example for the :class:`ThreadingMixIn` class:: # 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 |