Created
January 23, 2019 07:19
-
-
Save damianmoore/803c38409573f1d2eb7a43ded7173173 to your computer and use it in GitHub Desktop.
Python TCP server
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
# https://www.techbeamers.com/python-tutorial-write-tcp-server/ | |
import socketserver | |
class Handler_TCPServer(socketserver.BaseRequestHandler): | |
def handle(self): | |
# self.request - TCP socket connected to the client | |
self.data = self.request.recv(1024).strip() | |
print("{} sent:".format(self.client_address[0])) | |
print(self.data) | |
# just send back ACK for data arrival confirmation | |
self.request.sendall("ACK from TCP Server".encode()) | |
if __name__ == "__main__": | |
HOST, PORT = "localhost", 9999 | |
# Init the TCP server object, bind it to the localhost on 9999 port | |
tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer) | |
# Activate the TCP server. | |
# To abort the TCP server, press Ctrl-C. | |
tcp_server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment