首页 > 解决方案 > AttributeError:“str”对象没有属性“port”

问题描述

我正在尝试使用 python 构建一个聊天应用程序。

当我尝试运行程序时,我得到

AttributeError:“str”对象没有属性“port”

这是我的服务器代码:

import socket
import sys
import time

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 = conn.accept()
print(addr, "Has connected to the server and is online--")
print("")

这是我的客户代码:

import socket
import sys
import time

s = socket.socket()
host = input(str('Please enter the Hostname of the server: '))
port = 8080
s.connect((host.port)) 

标签: pythonsocketsattributeerror

解决方案


您调用该bind()函数的方式不正确。代替:

s.bind ((host.port))

它应该是:

s.bind((host,port)) 

看起来有点语法错误。


推荐阅读