Created
August 3, 2022 14:22
-
-
Save badri/d717e9c22672e7f89d962e49d64640bc to your computer and use it in GitHub Desktop.
TCP 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
| apiVersion: apps/v1 | |
| kind: Deployment | |
| metadata: | |
| name: iot-tcp | |
| labels: | |
| app: iot-tcp | |
| spec: | |
| replicas: 3 | |
| selector: | |
| matchLabels: | |
| app: iot-tcp | |
| template: | |
| metadata: | |
| labels: | |
| app: iot-tcp | |
| spec: | |
| containers: | |
| - name: tcp-server | |
| image: shapeblock/tcp:1.0 | |
| ports: | |
| - containerPort: 5000 | |
| name: tcp-server-port |
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
| FROM python:3.10-slim-buster | |
| WORKDIR /src | |
| #COPY requirements.txt . | |
| #RUN pip install -r requirements.txt | |
| COPY server2.py server2.py | |
| CMD ["python", "-u", "server2.py"] |
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 python | |
| import socket | |
| import sys | |
| from _thread import * | |
| ServerSideSocket = socket.socket() | |
| host = '0.0.0.0' | |
| port = 5000 | |
| ThreadCount = 0 | |
| try: | |
| ServerSideSocket.bind((host, port)) | |
| except socket.error as e: | |
| print(str(e)) | |
| print('Socket is listening..') | |
| ServerSideSocket.listen(5) | |
| def multi_threaded_client(connection): | |
| while True: | |
| data = connection.recv(2048) | |
| sys.stdout.buffer.write(data) | |
| #response = 'Server message: ' + data.decode('utf-8') | |
| if not data: | |
| break | |
| connection.send(data) | |
| #connection.sendall(str.encode(response)) | |
| connection.close() | |
| while True: | |
| Client, address = ServerSideSocket.accept() | |
| print('Connected to: ' + address[0] + ':' + str(address[1])) | |
| start_new_thread(multi_threaded_client, (Client, )) | |
| ThreadCount += 1 | |
| print('Thread Number: ' + str(ThreadCount)) | |
| ServerSideSocket.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
| apiVersion: v1 | |
| kind: Service | |
| metadata: | |
| name: iot-tcp | |
| spec: | |
| type: LoadBalancer | |
| selector: | |
| app: iot-tcp | |
| ports: | |
| - name: tcp-server-port | |
| protocol: TCP | |
| port: 5000 | |
| targetPort: tcp-server-port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment