You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python读取远程服务器文件 报错invalid literal for int()怎么解决

错误原因
  • fabric.network.connect 参数传参顺序不匹配,导致IP字符串被传入要求整数类型的端口参数位,触发类型转换报错
  • 原代码中port变量定义为字符串类型'3306',不符合端口参数的整数类型要求
  • 额外注意:3306是MySQL服务的默认端口,SSH远程连接默认端口为22,若未手动修改服务器SSH端口,原端口参数填写错误会导致后续连接失败
修复后代码
from contextlib     import closing
from fabric.network import connect
user = 'cortekr1_cuong' 
password = '12345678'
host = '103.227.176.29' 
# 确认服务器SSH端口,未修改过则填默认值22,且必须为整数类型
port = 22
remote_file = '/RBICloudv1/static/json/vietnamprovince/vn.json'
# 使用关键字参数明确指定各参数含义,避免顺序错配
with closing(connect(host=host, port=port, user=user, password=password)) as ssh, \
     closing(ssh.open_sftp()) as sftp, \
     closing(sftp.open(remote_file)) as file:
    for line in file:
      print(line) 

内容的提问来源于stack exchange,提问作者Vu Trong Toi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 17:39:02