Question: Modify the TCP server python code posted above as the following: Find digits in a message received in the TCP server posted above. A TCP

Modify the TCP server python code posted above as the following:

  1. Find digits in a message received in the TCP server posted above. A TCP client must send the TCP server a message containing alpha-numeric characters (A-Z, 0-9). The server finds only numerical characters in the message and sends the characters to the client.
  2. If the client sends exit, the connection between the client and server is terminated, and the server prints "goodbye".

# TCP Client Python code import socket import sys

# Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port on the server given by the caller server_address = (sys.argv[1], 10000) sys.stderr.write('connecting to %s port %s ' % server_address) sock.connect(server_address)

while True: try: message = sys.stdin.readline().replace(' ','') sys.stderr.write('sending %s ' % message) sock.sendall(message.encode())

if message.lower() == 'exit': sock.close() break

#amount_received = 0 #amount_expected = len(message) #while amount_received < amount_expected: data = sock.recv(16) amount_received = len(data.decode()) sys.stderr.write('received "%s" ' % data.decode())

except: sock.close() break

___________________________________________________________________________

# TCP Server Python code import socket import sys

# Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the address given on the command line server_address = ('', 10000) sock.bind(server_address) sys.stderr.write('starting up on %s port %s ' % sock.getsockname()) sock.listen(1)

while True: sys.stderr.write('waiting for a connection ') connection, client_address = sock.accept() try: print 'client connected: address ', client_address while True: data = connection.recv(16) sys.stderr.write('received "%s" ' % data.decode()) if data: # Find digits in data connection.sendall(data) else: break finally: connection.close()

* JUST A NOTE *: The client code, I think it does not need anything to add. Only the server code needs some addition based on the question.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!