Created
July 23, 2017 21:12
-
-
Save dwoz/3f7cf98cf1276896bef72c7e56988812 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# simple_tcp_client.py | |
import socket | |
TCP_IP = '127.0.0.1' | |
TCP_PORT = 5005 | |
BUFFER_SIZE = 1024 | |
MESSAGE = b"Hello, World!" | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((TCP_IP, TCP_PORT)) | |
s.send(MESSAGE) | |
data = s.recv(BUFFER_SIZE) | |
s.close() | |
print("received data: {}".format(data)) | |
# simple_tcp_server.py | |
import socket | |
TCP_IP = '127.0.0.1' | |
TCP_PORT = 5005 | |
BUFFER_SIZE = 20 # Normally 1024, but we want fast response | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((TCP_IP, TCP_PORT)) | |
s.listen(1) | |
conn, addr = s.accept() | |
print('server - connection address: {}'.format(addr)) | |
while 1: | |
data = conn.recv(BUFFER_SIZE) | |
if not data: break | |
print("server - received data: {}".format(data)) | |
conn.send(data) # echo | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment