@@ -361,7 +361,7 @@ This is the server side::
361361 def handle(self):
362362 # self.request is the TCP socket connected to the client
363363 self.data = self.request.recv(1024).strip()
364- print("%s wrote:" % self.client_address[0])
364+ print("{} wrote:".format( self.client_address[0]) )
365365 print(self.data)
366366 # just send back the same data, but upper-cased
367367 self.request.send(self.data.upper())
@@ -385,7 +385,7 @@ objects that simplify communication by providing the standard file interface)::
385385 # self.rfile is a file-like object created by the handler;
386386 # we can now use e.g. readline() instead of raw recv() calls
387387 self.data = self.rfile.readline().strip()
388- print("%s wrote:" % self.client_address[0])
388+ print("{} wrote:".format( self.client_address[0]) )
389389 print(self.data)
390390 # Likewise, self.wfile is a file-like object used to write back
391391 # to the client
@@ -408,16 +408,18 @@ This is the client side::
408408 # Create a socket (SOCK_STREAM means a TCP socket)
409409 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
410410
411- # Connect to server and send data
412- sock.connect((HOST, PORT))
413- sock.send(bytes(data + "\n","utf8"))
411+ try:
412+ # Connect to server and send data
413+ sock.connect((HOST, PORT))
414+ sock.send(bytes(data + "\n", "utf-8"))
414415
415- # Receive data from the server and shut down
416- received = sock.recv(1024)
417- sock.close()
416+ # Receive data from the server and shut down
417+ received = str(sock.recv(1024), "utf-8")
418+ finally:
419+ sock.close()
418420
419- print("Sent: %s" % data)
420- print("Received: %s" % received)
421+ print("Sent: {}".format( data) )
422+ print("Received: {}".format( received) )
421423
422424
423425The output of the example should look something like this:
@@ -434,10 +436,10 @@ Client::
434436
435437 $ python TCPClient.py hello world with TCP
436438 Sent: hello world with TCP
437- Received: b' HELLO WORLD WITH TCP'
439+ Received: HELLO WORLD WITH TCP
438440 $ python TCPClient.py python is nice
439441 Sent: python is nice
440- Received: b' PYTHON IS NICE'
442+ Received: PYTHON IS NICE
441443
442444
443445:class: `socketserver.UDPServer ` Example
@@ -458,7 +460,7 @@ This is the server side::
458460 def handle(self):
459461 data = self.request[0].strip()
460462 socket = self.request[1]
461- print("%s wrote:" % self.client_address[0])
463+ print("{} wrote:".format( self.client_address[0]) )
462464 print(data)
463465 socket.sendto(data.upper(), self.client_address)
464466
@@ -480,11 +482,11 @@ This is the client side::
480482
481483 # As you can see, there is no connect() call; UDP has no connections.
482484 # Instead, data is directly sent to the recipient via sendto().
483- sock.sendto(bytes(data + "\n","utf8 "), (HOST, PORT))
484- received = sock.recv(1024)
485+ sock.sendto(bytes(data + "\n", "utf-8 "), (HOST, PORT))
486+ received = str( sock.recv(1024), "utf-8" )
485487
486- print("Sent: %s" % data)
487- print("Received: %s" % received)
488+ print("Sent: {}".format( data) )
489+ print("Received: {}".format( received) )
488490
489491The output of the example should look exactly like for the TCP server example.
490492
@@ -504,9 +506,9 @@ An example for the :class:`ThreadingMixIn` class::
504506 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
505507
506508 def handle(self):
507- data = self.request.recv(1024)
509+ data = str( self.request.recv(1024), 'ascii' )
508510 cur_thread = threading.current_thread()
509- response = bytes("%s: %s" % (cur_thread.getName() , data),'ascii')
511+ response = bytes("{}: {}".format (cur_thread.name , data), 'ascii')
510512 self.request.send(response)
511513
512514 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@@ -515,10 +517,12 @@ An example for the :class:`ThreadingMixIn` class::
515517 def client(ip, port, message):
516518 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
517519 sock.connect((ip, port))
518- sock.send(message)
519- response = sock.recv(1024)
520- print("Received: %s" % response)
521- sock.close()
520+ try:
521+ sock.send(bytes(message, 'ascii'))
522+ response = str(sock.recv(1024), 'ascii')
523+ print("Received: {}".format(response))
524+ finally:
525+ sock.close()
522526
523527 if __name__ == "__main__":
524528 # Port 0 means to select an arbitrary unused port
@@ -531,13 +535,13 @@ An example for the :class:`ThreadingMixIn` class::
531535 # more thread for each request
532536 server_thread = threading.Thread(target=server.serve_forever)
533537 # Exit the server thread when the main thread terminates
534- server_thread.setDaemon( True)
538+ server_thread.daemon = True
535539 server_thread.start()
536540 print("Server loop running in thread:", server_thread.name)
537541
538- client(ip, port, b "Hello World 1")
539- client(ip, port, b "Hello World 2")
540- client(ip, port, b "Hello World 3")
542+ client(ip, port, "Hello World 1")
543+ client(ip, port, "Hello World 2")
544+ client(ip, port, "Hello World 3")
541545
542546 server.shutdown()
543547
@@ -546,9 +550,9 @@ The output of the example should look something like this::
546550
547551 $ python ThreadedTCPServer.py
548552 Server loop running in thread: Thread-1
549- Received: b" Thread-2: b' Hello World 1'"
550- Received: b" Thread-3: b' Hello World 2'"
551- Received: b" Thread-4: b' Hello World 3'"
553+ Received: Thread-2: Hello World 1
554+ Received: Thread-3: Hello World 2
555+ Received: Thread-4: Hello World 3
552556
553557
554558The :class: `ForkingMixIn ` class is used in the same way, except that the server
0 commit comments