尝试获取用户的主机名时,我遇到了奇怪的 TypeError。
def getHostName(tfm, username):
found = False
for room in tfm.rooms.values():
for playerCode, client in room.clients.items():
if client.username == username:
found = socket.gethostbyaddr(socket.gethostname())[0]
break
return found
这是我用来获取主机名的内容,这就是它的错误所在。
def getIPaddress(tfm, username):
found = False
for room in tfm.rooms.values():
for playerCode, client in room.clients.items():
if client.username == username:
found = client.address[0]
break
return found
这就是我用来成功获取IP地址的方法。
Traceback (most recent call last):
File "Wesley.py", line 544, in found_terminator
tfm.parsePacket(packet, MDT, Size)
File "Wesley.py", line 760, in parsePacket
tfm.parseDataUTF(packet[2:struct.unpack('!h', packet[:2])[0]+2])
File "Wesley.py", line 1063, in parseDataUTF
Commands(tfm, values[0], values)
File "/root/Server/Commands.py", line 2543, in ParseCommand
hostname = tfm.server.getHostName(username)
File "Wesley.py", line 5757, in getHostName
found = socket.gethostbyaddr(socket.gethostname())[0]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
我使用命令来指定用户名。