无法在Python中拆分元组对象:';元组';对象没有属性';拆分';
当我尝试运行我的程序时,出现如下错误:无法在Python中拆分元组对象:';元组';对象没有属性';拆分';,python,sockets,split,tuples,Python,Sockets,Split,Tuples,当我尝试运行我的程序时,出现如下错误: Traceback (most recent call last): File "/Volumes/USER/server.py", line 15, in <module> filename = message.split()[1] AttributeError: 'tuple' object has no attribute 'split' 在与程序server.py相同的目录中,我有一个名为helloworld.ht
Traceback (most recent call last):
File "/Volumes/USER/server.py", line 15, in <module>
filename = message.split()[1]
AttributeError: 'tuple' object has no attribute 'split'
在与程序server.py相同的目录中,我有一个名为helloworld.html的文件,当我使用硬编码端口转到服务器的IP地址时,应该加载该文件,当我转到一个不存在的文件时,也会显示404。ex(192.168.1.2:1234/helloworld.html):
socket.accept()
接受连接。套接字必须绑定到地址并侦听连接。返回值是一对(conn,address)
,其中conn是可用于在连接上发送和接收数据的新套接字对象,address是绑定到连接另一端的套接字的地址
所以,在你的代码中,在你这样做之后
message = connectionSocket.recvfrom(1024)
filename = message.split()
消息
成为元组(连接,地址)
现在,元组没有方法split()
。只有字符串具有该方法。我认为您要做的是拆分地址-因此必须首先获取元组的第二个元素(当然是字符串),然后拆分地址:
recvfrom()
返回一个元组,而元组不理解split()
。
message = connectionSocket.recvfrom(1024)
filename = message.split()
filename = message[1].split()