Sockets python 3.5.2 UdpServer sendto()错误:模块';插座';没有属性';发送至';
我在Sockets python 3.5.2 UdpServer sendto()错误:模块';插座';没有属性';发送至';,sockets,python-3.x,networking,attributes,Sockets,Python 3.x,Networking,Attributes,我在s.sendto(data,addr) 我的代码是这样的 import socket def Main(): host = '127.0.0.1' port = 5000 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((host, port)) print("server started") while True:
s.sendto(data,addr)
我的代码是这样的
import socket
def Main():
host = '127.0.0.1'
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
print("server started")
while True:
data, addr = s.recvfrom(1024)
print ("message from : "+ str(addr))
print ("from connected user : "+ str(data))
data = str(data.upper())
print ("sending : "+ str(data))
socket.sendto(data, addr)
和结果
socket.sendto(data, addr)
AttributeError: module 'socket' has no attribute 'sendto'
s.close()
if __name__ == '__main__':
Main()
在UdpClient
s.sendto
正在工作时,似乎您键入了socket.sendto(…
语句:由于要从类socket.socket
的实例调用方法sendto(),因此引发了AttributeError,而不是来自套接字
模块本身。有关该语句含义的更多详细信息,请参阅
因此,您基本上需要将socket.sendto(…更改为s.sendto(…
此外,如果要检查任何x
对象的属性(除了读取文档外),只需检查其x.\uu dict\uuu
字段,如前所述
干杯嗨!没有测试它的愚蠢问题…难道不是
s.sendto()
而不是socket.sendto()
?非常感谢你