gpt4 book ai didi

swift - 在 Swift 中使用 NMSSH 通过 sftp 检索数据

转载 作者:行者123 更新时间:2023-12-05 07:14:38 25 4
gpt4 key购买 nike

我正在尝试从我的服务器获取一个 JSON 文件到我的 iPhone。我将 NMSSH 用于我的 FTP 连接并且连接有效。

但我无法从我的服务器检索任何数据。无论我使用什么路径 ~///someFolder/someFolder 都没有关系。每次,我都会收到错误:

NMSSH_Test[] NMSSH: Could not open file at path /testDirectory (Error 0: )

这里是我的代码:

class Data {
init() {
let session = NMSSHSession.init(host: "someIp", andUsername: "user")
session.connect()
if session.isConnected{
session.authenticate(byPassword: "password")
if session.isAuthorized {
let sftpsession = NMSFTP(session : session)
sftpsession.contents(atPath: "/testDirectory")
// if (sftpsession.contentsOfDirectory(atPath: "~/") != nil) {
// print("Found Data!")
// }
}
}
}
}

最佳答案

试试下面的代码:

let session = NMSSHSession(host: serverHost, port: portNo, andUsername: serverUsername)
session.connect()
if session.isConnected{
let privateKeypath:String = Bundle.main.path(forResource: "mykey", ofType: "")!
let privateKey: String = try! String(contentsOfFile: privateKeypath, encoding: String.Encoding.utf8)

session.authenticateBy(inMemoryPublicKey: "", privateKey: privateKey, andPassword: passwordStr)

if session.isAuthorized == true {
let sftpsession = NMSFTP(session: session)
sftpsession.connect()
if sftpsession.isConnected {
print("download session is connected")
}
else {
print("Error: download session not connected")
return
}
guard let directoryContents = downloadSession.contentsOfDirectory(atPath: "Your Directory Path goes here!")
else {
print("Unable to read contents of file in SSH site")
return
}
print("Data at specified path captured: " + directoryContents)

guard let contentList = downloadSession.contents(atPath: "Enter your path here!")
else {
print("Unable to read contents of file in SSH site")
return
}
print("Printing Contents of file at path")
print(contentList)
}
}
}

关于swift - 在 Swift 中使用 NMSSH 通过 sftp 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59847447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com