8000 gh-112020: socketserver: Add an example for keeping the connection open by talcs · Pull Request #112054 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112020: socketserver: Add an example for keeping the connection open #112054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Doc/library/socketserver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,24 @@ The difference is that the ``readline()`` call in the second handler will call
single ``recv()`` call in the first handler will just return what has been sent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the line above add "or ``recv()`` returns an empty bytes object" after "until it encounters a newline character"

from the client in one ``sendall()`` call.

The handlers presented above close the connection after handling a single request.
It is possible, however, to keep the connection open to handle many requests,
until the client hangs up::

class MyTCPHandler(socketserver.BaseRequestHandler):

def handle(self):
while True:
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024)
# `socket.recv` indicates that the client has hung up by returning 0 bytes
if len(self.data) == 0:
return
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())


This is the client side::

Expand Down
0