Last active
May 22, 2018 04:17
-
-
Save pythonsuezo/3fa03beff2f2651c220bf22293246350 to your computer and use it in GitHub Desktop.
エコーサーバーとクライアントのコード
This file contains 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
# Echo server program | |
import socket | |
HOST = '' # Symbolic name meaning all available interfaces | |
PORT = 50007 # Arbitrary non-privileged port | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
conn, addr = s.accept() | |
with conn: | |
print('Connected by', addr) | |
while True: | |
data = conn.recv(1024) | |
if not data: break | |
conn.sendall(data) |
This file contains 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
# Echo client program | |
import socket | |
HOST = 'localhost' # The remote host | |
PORT = 50007 # The same port as used by the server | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.connect((HOST, PORT)) | |
s.sendall(b'Hello, world') | |
data = s.recv(1024) | |
print('Received', repr(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment