Question

Use Python TCP socket to implement an application with client-server architecture. In this application, client must...

Use Python TCP socket to implement an application with client-server architecture. In this application, client must read a line of characters from its input and send it to the server. The server must remove all non-alphanumeric characters from the input and send the modified data to the client. The client must receive the modified data and displays it on its screen.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Plese find below the code,

# save the below code as server.py

import socket

def remove_non_alphanumeric_characters(msg):
    """
    function to remove the nin alphanumeric chars from cleient message
    """
    return "".join([x for x in msg if x.isalnum()])
s = socket.socket()
port = 30013 #port number for connecting to server
host = socket.gethostname() # default localhost
s.bind((host, port)) # binding host with port
s.listen(5) # listening the client to connect
print("server listening.........\n")
while True:
   client, address = s.accept()
   client_msg = str(client.recv(1024).decode())
   print("Message received from client : ", client_msg)
   # removing the special character and sending to client
   client.send(remove_non_alphanumeric_characters(client_msg).encode())
   client.close()

# save the below code as client.py

import socket
# """ socket object """
s = socket.socket()
# port number for connection
port = 30013
host = socket.gethostname() # default hostname
s.connect((host, port)) # getting connection from host
msg = input("Enter the message  : ")
s.send(msg.encode()) # message sent to server
filtered_message  = s.recv(1024).decode()
print("Message after removing the non-alphanumeric charcaters : ",filtered_message)
s.close() # closing the connection

#first run the server.py and in another terminal/console run the client.py

#output

➜ python server.py
server listening.........

Message received from client : this_is_#message!
Message received from client : this_is@@@another.msg$#

➜ python client.py
Enter the message : this_is_#message!   
Message after removing the non-alphanumeric charcaters : thisismessage


➜ python client.py
Enter the message : this_is@@@another.msg$#
Message after removing the non-alphanumeric charcaters : thisisanothermsg

Add a comment
Know the answer?
Add Answer to:
Use Python TCP socket to implement an application with client-server architecture. In this application, client must...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT