'str'对象没有属性'port'

问题描述 投票:0回答:2

我正在编写此代码来创建聊天应用程序,但收到属性错误。请帮我解决一下。

import socket
import sys
import time

## end of imports

## init

s = socket.socket()
host = socket.gethostname()
print(" server will start on host", host)

port = 8080
s.bind((host.port))
print("")
print("Server done binding to host and port successfully")
print("")

s.listen(1)
conn, addr = s.accept()
print(addr, "has connected to the server and is now online")
print("")
python python-3.x
2个回答
0
投票

在这一行中:

s.bind((host.port))

您正在尝试调用对象“host”上名为“port”的属性,但在代码的前面您将“host”设置为:

host = socket.gethostname()

它只是返回一个字符串...它没有属性“port”,就像错误所说的那样。以下是您应该将“s.bind((host.port))”行更改为...

s.bind(port)

0
投票

它说“TypeError:bind():AF_INET地址必须是元组,而不是int”我需要做什么

© www.soinside.com 2019 - 2024. All rights reserved.