0

I send some message 'hello' from client to server using this command on server

    #Send some data to remote server
    message = 'hello'

    try :

    s.send(message)
    print 'data sent successfully'
    except socket.error:
    #Send failed
    print 'Send failed'

Now on server side , I want to check whether this message is present as a key in dictionary which is created on server side.

    msg=c.recvfrom(1024)

     if msg in data2.keys():
 print("key for this msg exists", msg)
     else:
     print("no such key exists",msg)

Now, the problem is it always says no such key exits. When I print the msg on server side which I have got from client.It comes out to be:

   ('hello', None)

I don't get it why it gives None along with hello.

because of this I am not even getting a match in dictionary. Please tell me where I am doing wrong.

2 Answers 2

2

As the documentation for socket states: The return value is a pair (string, address) where string is a string representing the data received and address is the address of the socket sending the data.

So your ('hello', None) is this pair and if you want to pick the message string from it to search it in your dict, you have to use msg[0].

Sign up to request clarification or add additional context in comments.

Comments

1

If you look at the documentation for socket.recvfrom() you will see that it returns a tuple. If you only want the data portion, you're better off just calling socket.recv().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.