Skip to content

Instantly share code, notes, and snippets.

@pythonsuezo
Last active May 22, 2018 04:17
Show Gist options
  • Save pythonsuezo/3fa03beff2f2651c220bf22293246350 to your computer and use it in GitHub Desktop.
Save pythonsuezo/3fa03beff2f2651c220bf22293246350 to your computer and use it in GitHub Desktop.
エコーサーバーとクライアントのコード
# 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)
# 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