Repository navigation
bpo-36889: Document Stream and StreamServer. #14203
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fee4b78
Rewrite examples using streams
tirkarthi 2d3056c
Document connect, connect_unix, StreamServer and Stream
tirkarthi 29938fc
Fix doc errors
tirkarthi 105e983
Use context manager in examples. Document connect_read_pipe, connect_…
tirkarthi c16357a
Use context manager
tirkarthi 274f484
Document StreamMode and add more doc for parameters
tirkarthi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Rewrite examples using streams
- Loading branch information
commit fee4b7809103007656e35727390c54e1b409e6d5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,17 +18,16 @@ streams:: | |
| import asyncio | ||
|
|
||
| async def tcp_echo_client(message): | ||
| reader, writer = await asyncio.open_connection( | ||
| '127.0.0.1', 8888) | ||
| stream = await asyncio.connect('127.0.0.1', 8888) | ||
|
|
||
| print(f'Send: {message!r}') | ||
| await writer.write(message.encode()) | ||
| stream.write(message.encode()) | ||
|
tirkarthi marked this conversation as resolved.
Outdated
|
||
|
|
||
| data = await reader.read(100) | ||
| data = await stream.read(100) | ||
| print(f'Received: {data.decode()!r}') | ||
|
|
||
| print('Close the connection') | ||
| await writer.close() | ||
| await stream.close() | ||
|
|
||
| asyncio.run(tcp_echo_client('Hello World!')) | ||
|
|
||
|
|
@@ -366,22 +365,21 @@ Examples | |
| TCP echo client using streams | ||
| ----------------------------- | ||
|
|
||
| TCP echo client using the :func:`asyncio.open_connection` function:: | ||
| TCP echo client using the :func:`asyncio.connect` function:: | ||
|
|
||
| import asyncio | ||
|
|
||
| async def tcp_echo_client(message): | ||
| reader, writer = await asyncio.open_connection( | ||
| '127.0.0.1', 8888) | ||
| stream = await asyncio.connect('127.0.0.1', 8888) | ||
|
tirkarthi marked this conversation as resolved.
Outdated
|
||
|
|
||
| print(f'Send: {message!r}') | ||
| writer.write(message.encode()) | ||
| stream.write(message.encode()) | ||
|
tirkarthi marked this conversation as resolved.
Outdated
|
||
|
|
||
| data = await reader.read(100) | ||
| data = await stream.read(100) | ||
| print(f'Received: {data.decode()!r}') | ||
|
|
||
| print('Close the connection') | ||
| writer.close() | ||
| await stream.close() | ||
|
|
||
| asyncio.run(tcp_echo_client('Hello World!')) | ||
|
|
||
|
|
@@ -397,28 +395,28 @@ TCP echo client using the :func:`asyncio.open_connection` function:: | |
| TCP echo server using streams | ||
| ----------------------------- | ||
|
|
||
| TCP echo server using the :func:`asyncio.start_server` function:: | ||
| TCP echo server using the :class:`asyncio.StreamServer` class:: | ||
|
|
||
| import asyncio | ||
|
|
||
| async def handle_echo(reader, writer): | ||
| data = await reader.read(100) | ||
| async def handle_echo(stream): | ||
| data = await stream.read(100) | ||
| message = data.decode() | ||
| addr = writer.get_extra_info('peername') | ||
| addr = stream.get_extra_info('peername') | ||
|
|
||
| print(f"Received {message!r} from {addr!r}") | ||
|
|
||
| print(f"Send: {message!r}") | ||
| writer.write(data) | ||
| await writer.drain() | ||
| stream.write(data) | ||
|
tirkarthi marked this conversation as resolved.
Outdated
|
||
| await stream.drain() | ||
|
|
||
| print("Close the connection") | ||
| writer.close() | ||
| await stream.close() | ||
|
|
||
| async def main(): | ||
|
tirkarthi marked this conversation as resolved.
|
||
| server = await asyncio.start_server( | ||
| handle_echo, '127.0.0.1', 8888) | ||
| server = asyncio.StreamServer(handle_echo, '127.0.0.1', 8888) | ||
|
|
||
| await server.start_serving() | ||
| addr = server.sockets[0].getsockname() | ||
| print(f'Serving on {addr}') | ||
|
|
||
|
|
@@ -446,21 +444,19 @@ Simple example querying HTTP headers of the URL passed on the command line:: | |
| async def print_http_headers(url): | ||
| url = urllib.parse.urlsplit(url) | ||
| if url.scheme == 'https': | ||
| reader, writer = await asyncio.open_connection( | ||
| url.hostname, 443, ssl=True) | ||
| stream = await asyncio.connect(url.hostname, 443, ssl=True) | ||
| else: | ||
| reader, writer = await asyncio.open_connection( | ||
| url.hostname, 80) | ||
| stream = await asyncio.connect(url.hostname, 80) | ||
|
|
||
| query = ( | ||
| f"HEAD {url.path or '/'} HTTP/1.0\r\n" | ||
| f"Host: {url.hostname}\r\n" | ||
| f"\r\n" | ||
| ) | ||
|
|
||
| writer.write(query.encode('latin-1')) | ||
| stream.write(query.encode('latin-1')) | ||
|
tirkarthi marked this conversation as resolved.
|
||
| while True: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some PEP 572 related example. while True:
line = await stream.readline()
if not line:
break
line = line.decode('latin1').rstrip()
if line:
print(f'HTTP header> {line}')while (line := await stream.readline()):
line = line.decode('latin1').rstrip()
if line:
print(f'HTTP header> {line}')
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Agree, |
||
| line = await reader.readline() | ||
| line = await stream.readline() | ||
| if not line: | ||
| break | ||
|
|
||
|
|
@@ -469,7 +465,7 @@ Simple example querying HTTP headers of the URL passed on the command line:: | |
| print(f'HTTP header> {line}') | ||
|
|
||
| # Ignore the body, close the socket | ||
| writer.close() | ||
| await stream.close() | ||
|
|
||
| url = sys.argv[1] | ||
| asyncio.run(print_http_headers(url)) | ||
|
|
@@ -490,7 +486,7 @@ Register an open socket to wait for data using streams | |
| ------------------------------------------------------ | ||
|
|
||
| Coroutine waiting until a socket receives data using the | ||
| :func:`open_connection` function:: | ||
| :func:`asyncio.connect` function:: | ||
|
|
||
| import asyncio | ||
| import socket | ||
|
|
@@ -504,17 +500,17 @@ Coroutine waiting until a socket receives data using the | |
| rsock, wsock = socket.socketpair() | ||
|
|
||
| # Register the open socket to wait for data. | ||
| reader, writer = await asyncio.open_connection(sock=rsock) | ||
| stream = await asyncio.connect(sock=rsock) | ||
|
|
||
| # Simulate the reception of data from the network | ||
| loop.call_soon(wsock.send, 'abc'.encode()) | ||
|
|
||
| # Wait for data | ||
| data = await reader.read(100) | ||
| data = await stream.read(100) | ||
|
|
||
| # Got data, we are done: close the socket | ||
| print("Received:", data.decode()) | ||
| writer.close() | ||
| await stream.close() | ||
|
|
||
| # Close the second socket | ||
| wsock.close() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.