Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b0ba0425
Commit
b0ba0425
authored
Oct 12, 2014
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asyncio doc: enhance TCP client example
parent
ff6fc56f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
7 deletions
+12
-7
Doc/library/asyncio-protocol.rst
Doc/library/asyncio-protocol.rst
+12
-7
No files found.
Doc/library/asyncio-protocol.rst
View file @
b0ba0425
...
...
@@ -447,21 +447,26 @@ TCP echo client example, send data and wait until the connection is closed::
import asyncio
class EchoClientProtocol(asyncio.Protocol):
message = 'This is the message. It will be echoed.'
def __init__(self, message, loop):
self.message = message
self.loop = loop
def connection_made(self, transport):
transport.write(self.message.encode())
print('
data sent: {
}'.format(self.message))
print('
Data sent: {!r
}'.format(self.message))
def data_received(self, data):
print('
data received: {
}'.format(data.decode()))
print('
Data received: {!r
}'.format(data.decode()))
def connection_lost(self, exc):
print('server closed the connection')
asyncio.get_event_loop().stop()
print('The server closed the connection')
print('Stop the event lop')
self.loop.stop()
loop = asyncio.get_event_loop()
coro = loop.create_connection(EchoClientProtocol, '127.0.0.1', 8888)
message = 'Hello World!'
coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
'127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
...
...
@@ -494,7 +499,7 @@ TCP echo server example, send back received data and close the connection::
print('Send: {!r}'.format(message))
self.transport.write(data)
print('Close the socket')
print('Close the
client
socket')
self.transport.close()
loop = asyncio.get_event_loop()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment