Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
ed051594
Kaydet (Commit)
ed051594
authored
Eki 12, 2014
tarafından
Victor Stinner
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
asyncio doc: add TCP echo client/server using streams
üst
75678658
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
8 deletions
+102
-8
asyncio-protocol.rst
Doc/library/asyncio-protocol.rst
+24
-8
asyncio-stream.rst
Doc/library/asyncio-stream.rst
+78
-0
No files found.
Doc/library/asyncio-protocol.rst
Dosyayı görüntüle @
ed051594
...
...
@@ -439,10 +439,13 @@ coroutine can be used to wait until the write buffer is flushed.
Protocol examples
=================
TCP echo client
---------------
.. _asyncio-tcp-echo-client-protocol:
TCP echo client example, send data and wait until the connection is closed::
TCP echo client protocol
------------------------
TCP echo client using the :meth:`BaseEventLoop.create_connection` method, send
data and wait until the connection is closed::
import asyncio
...
...
@@ -478,11 +481,19 @@ having to write a short coroutine to handle the exception and stop the
running loop. At :meth:`~BaseEventLoop.run_until_complete` exit, the loop is
no longer running, so there is no need to stop the loop in case of an error.
.. seealso::
The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>`
example uses the :func:`asyncio.open_connection` function.
TCP echo server
---------------
.. _asyncio-tcp-echo-server-protocol:
TCP echo server example, send back received data and close the connection::
TCP echo server protocol
------------------------
TCP echo server using the :meth:`BaseEventLoop.create_server` method, send back
received data and close the connection::
import asyncio
...
...
@@ -507,12 +518,12 @@ TCP echo server example, send back received data and close the connection::
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)
# Serve
r
requests until CTRL+c is pressed
# Serve requests until CTRL+c is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
p
rint("exit")
p
ass
# Close the server
server.close()
...
...
@@ -524,6 +535,11 @@ TCP echo server example, send back received data and close the connection::
methods are asynchronous. ``yield from`` is not needed because these transport
methods are not coroutines.
.. seealso::
The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>`
example uses the :func:`asyncio.start_server` function.
.. _asyncio-udp-echo-client-protocol:
...
...
Doc/library/asyncio-stream.rst
Dosyayı görüntüle @
ed051594
...
...
@@ -241,6 +241,84 @@ IncompleteReadError
Stream examples
===============
.. _asyncio-tcp-echo-client-streams:
TCP echo client using streams
-----------------------------
TCP echo client using the :func:`asyncio.open_connection` function::
import asyncio
def tcp_echo_client(message, loop):
reader, writer = yield from asyncio.open_connection('127.0.0.1', 8888,
loop=loop)
print('Send: %r' % message)
writer.write(message.encode())
data = yield from reader.read(100)
print('Received: %r' % data.decode())
print('Close the socket')
writer.close()
message = 'Hello World!'
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(message, loop))
loop.close()
.. seealso::
The :ref:`TCP echo client protocol <asyncio-tcp-echo-client-protocol>`
example uses the :meth:`BaseEventLoop.create_connection` method.
.. _asyncio-tcp-echo-server-streams:
TCP echo server using streams
-----------------------------
TCP echo server using the :func:`asyncio.start_server` function::
import asyncio
@asyncio.coroutine
def handle_echo(reader, writer):
data = yield from reader.read(100)
message = data.decode()
addr = writer.get_extra_info('peername')
print("Received %r from %r" % (message, addr))
print("Send: %r" % message)
writer.write(data)
yield from writer.drain()
print("Close the client socket")
writer.close()
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
server = loop.run_until_complete(coro)
# Serve requests until CTRL+c is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
.. seealso::
The :ref:`TCP echo server protocol <asyncio-tcp-echo-server-protocol>`
example uses the :meth:`BaseEventLoop.create_server` method.
Get HTTP headers
----------------
...
...
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