Questions

Create a client server program in python to return a dictionary of count of vowels in a string.

0

In Python 3, create a client and a server. The server should create a TCP socket listening to the port 41922. The server should be expecting a string of characters and will return a dictionary containing the vowels in the string and the count of each vowel. Then the client should also connect to the port 41922, send the string and then receive the dictionary from the server and print it out in a nice format. Use the pickle module to convert the data that’s being transferred.

I have a server to test this out on so don’t worry about that, I’ll just specify that on my own program.



asked 1 year, 7 months ago
Reputation: 1





1 Answer

0

vowelServer.py

import socket,pickle

# Create socket object
serverSocket = socket.socket()

# Bind host and port
host = '127.0.0.1'
port = 41922
serverSocket.bind((host,port))

# Keep in listening state
serverSocket.listen(5)


while True:
    # accept connections now
    clientSocket, addr = serverSocket.accept()
    print ("Got Connection from", str(addr))

    # send connection confirmation message
    clientSocket.send("Thank You for connecting".encode())

    # while msg from client to use server
    while True:

        if clientSocket.recv(1024).decode()!="y":
            break

        # received the dump data
        recvdata = clientSocket.recv(4096)

        # Undump or load list received from client
        data = pickle.loads(recvdata)

        dict={}
        dict['a']=dict['e']=dict['i']=dict['o']=dict['u']=0


        for c in data:
            if c=='a':
                dict['a']+=1
            elif c=='e':
                dict['e']+=1
            elif c=='i':
                dict['i']+=1
            elif c=='o':
                dict['o']+=1
            elif c=='u':
                dict['u']+=1


        # dump and send it
        clientSocket.send(pickle.dumps(dict))

    # close the connection
    print("Connection closed from", str(addr))
    clientSocket.close()

vowelClient.py

import socket,pickle

# create socket object
clientSocket = socket.socket()

# Request Connection
host = '127.0.0.1'
port = 41922
clientSocket.connect((host,port))

# Get connection confirmation
print(clientSocket.recv(1024).decode())

while True:
    # Ask user input whether to continue or exit
    msg = input("\nEnter y to continue or n to exit : ")

    # send user response to server if response is no then client
    # get disconnected from server
    clientSocket.send(msg.encode())

    # If yes continue fetching data from server
    if (msg == "y"):

        # Ask user to enter list elements
        data = input("Enter the string: ")


        # dump it before sending using dumps()
        clientSocket.send(pickle.dumps(data))

        # received dumped data from server
        data1 = clientSocket.recv(1024)

        # befor printing undump or load data using loads()
        print("Vowels dictionary in given string: ")
        print(pickle.loads(data1))

    # close connection if answer is not yes
    else:
        print("Thank for connecting. See You soon!")
        break

clientSocket.close()

Output Screenshot

output.png

answered 1 year, 7 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  1 year, 7 months ago
Viewed:  795 times
Active:  1 year, 7 months ago