cpython: d34beaaf7060 (original) (raw)
--- a/Doc/library/socketserver.rst +++ b/Doc/library/socketserver.rst @@ -348,7 +348,7 @@ 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])[](#l1.7)
print("{} wrote:".format(self.client_address[0]))[](#l1.8) print(self.data)[](#l1.9) # just send back the same data, but upper-cased[](#l1.10) self.request.send(self.data.upper())[](#l1.11)
@@ -372,7 +372,7 @@ objects that simplify communication by p # 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])[](#l1.16)
print("{} wrote:".format(self.client_address[0]))[](#l1.17) print(self.data)[](#l1.18) # Likewise, self.wfile is a file-like object used to write back[](#l1.19) # to the client[](#l1.20)
@@ -395,16 +395,18 @@ This is the client side:: # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
# Connect to server and send data[](#l1.29)
sock.connect((HOST, PORT))[](#l1.30)
sock.send(bytes(data + "\n", "utf-8"))[](#l1.31)
# Receive data from the server and shut down[](#l1.36)
received = str(sock.recv(1024), "utf-8")[](#l1.37)
- finally:
sock.close()[](#l1.39)
The output of the example should look something like this: @@ -421,10 +423,10 @@ Client:: $ python TCPClient.py hello world with TCP Sent: hello world with TCP
:class:socketserver.UDPServer
Example
@@ -445,7 +447,7 @@ This is the server side::
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print("%s wrote:" % self.client_address[0])[](#l1.65)
print("{} wrote:".format(self.client_address[0]))[](#l1.66) print(data)[](#l1.67) socket.sendto(data.upper(), self.client_address)[](#l1.68)
@@ -467,11 +469,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().
The output of the example should look exactly like for the TCP server example. @@ -491,9 +493,9 @@ An example for the :class:`ThreadingMixI class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): def handle(self):
data = self.request.recv(1024)[](#l1.90)
data = str(self.request.recv(1024), 'ascii')[](#l1.91) cur_thread = threading.current_thread()[](#l1.92)
response = bytes("%s: %s" % (cur_thread.getName(), data),'ascii')[](#l1.93)
response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')[](#l1.94) self.request.send(response)[](#l1.95)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): @@ -502,10 +504,12 @@ An example for the :class:`ThreadingMixI def client(ip, port, message): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port))
sock.send(message)[](#l1.102)
response = sock.recv(1024)[](#l1.103)
print("Received: %s" % response)[](#l1.104)
sock.close()[](#l1.105)
try:[](#l1.106)
sock.send(bytes(message, 'ascii'))[](#l1.107)
response = str(sock.recv(1024), 'ascii')[](#l1.108)
print("Received: {}".format(response))[](#l1.109)
finally:[](#l1.110)
sock.close()[](#l1.111)
if name == "main": # Port 0 means to select an arbitrary unused port @@ -518,13 +522,13 @@ An example for the :class:`ThreadingMixI # 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)[](#l1.119)
server_thread.daemon = True[](#l1.120) server_thread.start()[](#l1.121) print("Server loop running in thread:", server_thread.name)[](#l1.122)
client(ip, port, b"Hello World 1")[](#l1.124)
client(ip, port, b"Hello World 2")[](#l1.125)
client(ip, port, b"Hello World 3")[](#l1.126)
client(ip, port, "Hello World 1")[](#l1.127)
client(ip, port, "Hello World 2")[](#l1.128)
client(ip, port, "Hello World 3")[](#l1.129)
server.shutdown() @@ -533,9 +537,9 @@ The output of the example should look so $ 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
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -190,6 +190,12 @@ Extension Modules