@@ -348,7 +348,7 @@ This is the server side::
348348 def handle(self):
349349 # self.request is the TCP socket connected to the client
350350 self.data = self.request.recv(1024).strip()
351- print("%s wrote:" % self.client_address[0])
351+ print("{} wrote:".format( self.client_address[0]) )
352352 print(self.data)
353353 # just send back the same data, but upper-cased
354354 self.request.send(self.data.upper())
@@ -372,7 +372,7 @@ objects that simplify communication by providing the standard file interface)::
372372 # self.rfile is a file-like object created by the handler;
373373 # we can now use e.g. readline() instead of raw recv() calls
374374 self.data = self.rfile.readline().strip()
375- print("%s wrote:" % self.client_address[0])
375+ print("{} wrote:".format( self.client_address[0]) )
376376 print(self.data)
377377 # Likewise, self.wfile is a file-like object used to write back
378378 # to the client
@@ -395,16 +395,18 @@ This is the client side::
395395 # Create a socket (SOCK_STREAM means a TCP socket)
396396 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
397397
398- # Connect to server and send data
399- sock.connect((HOST, PORT))
400- sock.send(bytes(data + "\n","utf8"))
398+ try:
399+ # Connect to server and send data
400+ sock.connect((HOST, PORT))
401+ sock.send(bytes(data + "\n", "utf-8"))
401402
402- # Receive data from the server and shut down
403- received = sock.recv(1024)
404- sock.close()
403+ # Receive data from the server and shut down
404+ received = str(sock.recv(1024), "utf-8")
405+ finally:
406+ sock.close()
405407
406- print("Sent: %s" % data)
407- print("Received: %s" % received)
408+ print("Sent: {}".format( data) )
409+ print("Received: {}".format( received) )
408410
409411
410412The output of the example should look something like this:
@@ -421,10 +423,10 @@ Client::
421423
422424 $ python TCPClient.py hello world with TCP
423425 Sent: hello world with TCP
424- Received: b' HELLO WORLD WITH TCP'
426+ Received: HELLO WORLD WITH TCP
425427 $ python TCPClient.py python is nice
426428 Sent: python is nice
427- Received: b' PYTHON IS NICE'
429+ Received: PYTHON IS NICE
428430
429431
430432:class: `socketserver.UDPServer ` Example
@@ -445,7 +447,7 @@ This is the server side::
445447 def handle(self):
446448 data = self.request[0].strip()
447449 socket = self.request[1]
448- print("%s wrote:" % self.client_address[0])
450+ print("{} wrote:".format( self.client_address[0]) )
449451 print(data)
450452 socket.sendto(data.upper(), self.client_address)
451453
@@ -467,11 +469,11 @@ This is the client side::
467469
468470 # As you can see, there is no connect() call; UDP has no connections.
469471 # Instead, data is directly sent to the recipient via sendto().
470- sock.sendto(bytes(data + "\n","utf8 "), (HOST, PORT))
471- received = sock.recv(1024)
472+ sock.sendto(bytes(data + "\n", "utf-8 "), (HOST, PORT))
473+ received = str( sock.recv(1024), "utf-8" )
472474
473- print("Sent: %s" % data)
474- print("Received: %s" % received)
475+ print("Sent: {}".format( data) )
476+ print("Received: {}".format( received) )
475477
476478The output of the example should look exactly like for the TCP server example.
477479
@@ -491,9 +493,9 @@ An example for the :class:`ThreadingMixIn` class::
491493 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
492494
493495 def handle(self):
494- data = self.request.recv(1024)
496+ data = str( self.request.recv(1024), 'ascii' )
495497 cur_thread = threading.current_thread()
496- response = bytes("%s: %s" % (cur_thread.getName() , data),'ascii')
498+ response = bytes("{}: {}".format (cur_thread.name , data), 'ascii')
497499 self.request.send(response)
498500
499501 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@@ -502,10 +504,12 @@ An example for the :class:`ThreadingMixIn` class::
502504 def client(ip, port, message):
503505 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
504506 sock.connect((ip, port))
505- sock.send(message)
506- response = sock.recv(1024)
507- print("Received: %s" % response)
508- sock.close()
507+ try:
508+ sock.send(bytes(message, 'ascii'))
509+ response = str(sock.recv(1024), 'ascii')
510+ print("Received: {}".format(response))
511+ finally:
512+ sock.close()
509513
510514 if __name__ == "__main__":
511515 # Port 0 means to select an arbitrary unused port
@@ -518,13 +522,13 @@ An example for the :class:`ThreadingMixIn` class::
518522 # more thread for each request
519523 server_thread = threading.Thread(target=server.serve_forever)
520524 # Exit the server thread when the main thread terminates
521- server_thread.setDaemon( True)
525+ server_thread.daemon = True
522526 server_thread.start()
523527 print("Server loop running in thread:", server_thread.name)
524528
525- client(ip, port, b "Hello World 1")
526- client(ip, port, b "Hello World 2")
527- client(ip, port, b "Hello World 3")
529+ client(ip, port, "Hello World 1")
530+ client(ip, port, "Hello World 2")
531+ client(ip, port, "Hello World 3")
528532
529533 server.shutdown()
530534
@@ -533,9 +537,9 @@ The output of the example should look something like this::
533537
534538 $ python ThreadedTCPServer.py
535539 Server loop running in thread: Thread-1
536- Received: b" Thread-2: b' Hello World 1'"
537- Received: b" Thread-3: b' Hello World 2'"
538- Received: b" Thread-4: b' Hello World 3'"
540+ Received: Thread-2: Hello World 1
541+ Received: Thread-3: Hello World 2
542+ Received: Thread-4: Hello World 3
539543
540544
541545The :class: `ForkingMixIn ` class is used in the same way, except that the server
0 commit comments