Created
February 28, 2019 11:44
-
-
Save natitomattis/85a36e75dc79960c5ca7119bd546e83e to your computer and use it in GitHub Desktop.
Python TCP client/server
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
| # Streaming Client | |
| import socket | |
| import time | |
| HOST = 'localhost' | |
| PORT = 50007 | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.connect((HOST, PORT)) | |
| while True: | |
| data = s.recv(1024) | |
| time.sleep(0.5) | |
| print data | |
| s.close() |
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
| # Streaming Server | |
| import socket | |
| import time | |
| from random import randint | |
| HOST = 'localhost' | |
| PORT = 50007 | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.bind((HOST, PORT)) | |
| s.listen(1) | |
| while True: | |
| conn, addr = s.accept() | |
| print 'Client connection accepted ', addr | |
| while True: | |
| try: | |
| data = str(randint(0, 9)) + "\n" | |
| print 'Server sent:', data | |
| conn.send(data) | |
| except socket.error, msg: | |
| print 'Client connection closed', addr | |
| break | |
| conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment