Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sooop
Last active June 19, 2020 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sooop/25eb013992c7b1a1dd7c74d96fe6abf6 to your computer and use it in GitHub Desktop.
Save sooop/25eb013992c7b1a1dd7c74d96fe6abf6 to your computer and use it in GitHub Desktop.
import asyncio
from random import random
_port = 7770
# asyncio socket server/client
async def run_client(host: str, port: int):
# 서버와의 연결을 생성합니다.
reader: asyncio.StreamReader
writer: asyncio.StreamWriter
reader, writer = await asyncio.open_connection(host, port)
# show connection info
print("[C] connected")
# 루프를 돌면서 입력받은 내용을 서버로 보내고,
# 응답을 받으면 출력합니다.
while True:
line = input("[C] enter message: ")
if not line:
break
# 입력받은 내용을 서버로 전송
payload = line.encode()
writer.write(payload)
await writer.drain()
print(f"[C] sent: {len(payload)} bytes.\n")
# 서버로부터 받은 응답을 표시
data = await reader.read(1024) # type: bytes
print(f"[C] received: {len(data)} bytes")
print(f"[C] message: {data.decode()}")
# 연결을 종료합니다.
print("[C] closing connection...")
writer.close()
await writer.wait_closed()
async def handler(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
while True:
# 클라이언트가 보낸 내용을 받기
data: bytes = await reader.read(1024)
# 받은 내용을 출력하고,
# 가공한 내용을 다시 내보내기
peername = writer.get_extra_info('peername')
print(f"[S] received: {len(data)} bytes from {peername}")
mes = data.decode()
print(f"[S] message: {mes}")
res = mes.upper()[::-1]
await asyncio.sleep(random() * 2)
writer.write(res.encode())
await writer.drain()
async def run_server():
# 서버를 생성하고 실행
server = await asyncio.start_server(handler, host="127.0.0.1", port=_port)
async with server:
# serve_forever()를 호출해야 클라이언트와 연결을 수락합니다.
await server.serve_forever()
async def main():
await asyncio.wait([run_server(), run_client("127.0.0.1", _port)])
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment