Browse Source

server and client initial commit

master
BuildTools 5 years ago
parent
commit
b9d5a799f4
  1. 20
      src/client.py
  2. 30
      src/server.py

20
src/client.py

@ -0,0 +1,20 @@
import socket
host_ip, server_port = "127.0.0.1", 9999
data = " Hello how are you?\n"
# Initialize a TCP client socket using SOCK_STREAM
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Establish connection to TCP server and exchange data
tcp_client.connect((host_ip, server_port))
tcp_client.sendall(data.encode())
# Read data from the TCP server and close the connection
received = tcp_client.recv(1024)
finally:
tcp_client.close()
print("Bytes Sent: {}".format(data))
print("Bytes Received: {}".format(received.decode()))

30
src/server.py

@ -0,0 +1,30 @@
import socketserver
class Handler_TCPServer(socketserver.BaseRequestHandler):
"""
The TCP Server class for demonstration.
Note: We need to implement the Handle method to exchange data
with TCP client.
"""
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()
Loading…
Cancel
Save