Last active
June 16, 2019 08:44
-
-
Save includeamin/feac687a9342c8f4b15f1b357293646e to your computer and use it in GitHub Desktop.
Simple echo server python socket
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
# Echo client program | |
import socket | |
HOST = 'localhost' | |
PORT = 3003 | |
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)) |
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
# Echo server program | |
import socket | |
HOST = '' | |
PORT = 3003 | |
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 | |
data = str(data)[2:-1].upper() | |
conn.sendall(data.encode()) | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment