【发布时间】:2021-01-07 01:51:17
【问题描述】:
所以我试图通过简单的 Almofire 网络调用连接我在 python(noob) 中编写的服务器端。
python代码是这样的:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))
s.listen(5)
while True:
# now our endpoint knows about the OTHER endpoint.
clientsocket, address = s.accept()
print(f"Connection from {address} has been established.")
快速是这样的:
func preformCall( success: @escaping () -> Void, failure: @escaping () -> Void) {
let url = "http://{my ip}:1234/"
Alamofire.request(url, method: .get).responseJSON { (response) in
if response.result.isFailure {
failure()
}
if let data = response.data {
let response = Response.init(data: data)
}
}
}
我的 ip - 来自网络首选项的 ip (mac) 我也连接到同一个wifi。
如果我在浏览器中使用相同的地址,我会在服务器端(终端)得到这个: 已建立来自 ('127.0.0.1', 52084) 的连接。 当我用模拟器设备连接到那里的服务器时,它成功(网址是 - 127.0.0.1:1234),但是当我尝试从真实设备连接时它失败并且我收到此错误:代码=-1004“无法连接到服务器。”
如何测试来自真实设备和 localhost 服务器的连接?
【问题讨论】:
-
你遇到了什么 alamofire 错误?
-
@EricHua Code=-1004 "无法连接到服务器。"
标签: python ios swift server localhost