python - 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))
解决方案
您调用该bind()
函数的方式不正确。代替:
s.bind ((host.port))
它应该是:
s.bind((host,port))
看起来有点语法错误。
推荐阅读
- asp.net-core - 只有多个托管服务中的第一个正在启动
- typescript - 在打字稿项目中使用 parser5
- firebase - React Native Firebase 在一个组件中显示两个集合(需要 JOIN 查询)
- python - Spyder (4.1.5) 代码单元格功能更改高亮外观
- sql-server - 带有字符串字段的 SQL 数据仓库外部表
- python - 尝试使用 JSON 获取 Pandas 的 API 数据时出错
- powershell - Powershell:查找文件,增量命名文件,然后将文件移动到指定位置
- python - 有没有办法对熊猫数据框中的一组重复项进行编号?
- html - bootstrap4-datetimepicker 时间选择器定位
- java - Android SQLite:我无法选择整数字段