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
ff6fc56f
Commit
ff6fc56f
authored
Oct 12, 2014
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asyncio doc: clarify how servers create protocol instances
parent
ec1d2431
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
6 deletions
+8
-6
Doc/library/asyncio-protocol.rst
Doc/library/asyncio-protocol.rst
+8
-6
No files found.
Doc/library/asyncio-protocol.rst
View file @
ff6fc56f
...
@@ -446,7 +446,7 @@ TCP echo client example, send data and wait until the connection is closed::
...
@@ -446,7 +446,7 @@ TCP echo client example, send data and wait until the connection is closed::
import asyncio
import asyncio
class EchoClient(asyncio.Protocol):
class EchoClient
Protocol
(asyncio.Protocol):
message = 'This is the message. It will be echoed.'
message = 'This is the message. It will be echoed.'
def connection_made(self, transport):
def connection_made(self, transport):
...
@@ -461,7 +461,7 @@ TCP echo client example, send data and wait until the connection is closed::
...
@@ -461,7 +461,7 @@ TCP echo client example, send data and wait until the connection is closed::
asyncio.get_event_loop().stop()
asyncio.get_event_loop().stop()
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
coro = loop.create_connection(EchoClient, '127.0.0.1', 8888)
coro = loop.create_connection(EchoClient
Protocol
, '127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_until_complete(coro)
loop.run_forever()
loop.run_forever()
loop.close()
loop.close()
...
@@ -481,7 +481,7 @@ TCP echo server example, send back received data and close the connection::
...
@@ -481,7 +481,7 @@ TCP echo server example, send back received data and close the connection::
import asyncio
import asyncio
class EchoServer(asyncio.Protocol):
class EchoServer
ClientProtocol
(asyncio.Protocol):
def connection_made(self, transport):
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
peername = transport.get_extra_info('peername')
print('Connection from {}'.format(peername))
print('Connection from {}'.format(peername))
...
@@ -498,7 +498,8 @@ TCP echo server example, send back received data and close the connection::
...
@@ -498,7 +498,8 @@ TCP echo server example, send back received data and close the connection::
self.transport.close()
self.transport.close()
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServer, '127.0.0.1', 8888)
# Each client connection will create a new protocol instance
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)
server = loop.run_until_complete(coro)
# Server requests until CTRL+c is pressed
# Server requests until CTRL+c is pressed
...
@@ -575,7 +576,7 @@ method, send back received data::
...
@@ -575,7 +576,7 @@ method, send back received data::
import asyncio
import asyncio
class EchoServer
Client
Protocol:
class EchoServerProtocol:
def connection_made(self, transport):
def connection_made(self, transport):
self.transport = transport
self.transport = transport
...
@@ -587,8 +588,9 @@ method, send back received data::
...
@@ -587,8 +588,9 @@ method, send back received data::
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
print("Starting UDP server")
print("Starting UDP server")
# One protocol instance will be created to serve all client requests
listen = loop.create_datagram_endpoint(
listen = loop.create_datagram_endpoint(
EchoServer
Client
Protocol, local_addr=('127.0.0.1', 9999))
EchoServerProtocol, local_addr=('127.0.0.1', 9999))
transport, protocol = loop.run_until_complete(listen)
transport, protocol = loop.run_until_complete(listen)
try:
try:
...
...
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