Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:.socket编程的send函数_Python_Sockets - Fatal编程技术网

Python:.socket编程的send函数

Python:.socket编程的send函数,python,sockets,Python,Sockets,我在服务器上使用此命令从客户端向服务器发送一些消息“hello” #Send some data to remote server message = 'hello' try : s.send(message) print 'data sent successfully' except socket.error: #Send failed print 'Send failed' 现在在服务器端,我想检查这个消息是否作为在服务器

我在服务器上使用此命令从客户端向服务器发送一些消息“hello”

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

    try :

    s.send(message)
    print 'data sent successfully'
    except socket.error:
    #Send failed
    print 'Send failed'
现在在服务器端,我想检查这个消息是否作为在服务器端创建的字典中的键存在

    msg=c.recvfrom(1024)

     if msg in data2.keys():
 print("key for this msg exists", msg)
     else:
     print("no such key exists",msg)
现在,问题是它总是说没有这样的关键出口。当我在服务器端打印从客户端获得的消息时,结果是:

   ('hello', None)
我不明白为什么它在打招呼的时候什么都不给


正因为如此,我在字典里连一个匹配词都找不到。请告诉我哪里做错了。

正如套接字文档所述:返回值是一对(字符串,地址),其中字符串是表示接收到的数据的字符串,地址是发送数据的套接字的地址


因此,您的
('hello',None)
就是这一对,如果您想从中选择消息字符串在dict中搜索它,您必须使用
msg[0]
作为套接字状态的文档:返回值是一对(字符串,地址)其中string是表示接收到的数据的字符串,address是发送数据的套接字的地址

因此,您的
('hello',None)
就是这一对,如果您想从中选取消息字符串在dict中搜索它,您必须使用
msg[0]
,如果您查看for
socket.recvfrom()
您将看到它返回一个元组。如果只需要数据部分,最好只调用
socket.recv()

如果查看for
socket.recvfrom()
您将看到它返回一个元组。如果只需要数据部分,最好只调用
socket.recv()