使用 Alamofire 连接到本地主机服务器(python)

Connecting to localhost server(python) using Alamofire

所以我正在尝试通过简单的 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.")

swift 是这样的:

   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) 的连接已建立。 同样,当我使用模拟器设备连接到服务器时它成功了(url 是 - 127.0.0.1:1234),但是当我尝试从真实设备连接时失败并且我收到此错误:代码=-1004“无法连接到服务器。"

如何测试来自真实设备和本地主机服务器的连接?

所以答案是听这个地址:0.0.0.0

     s.bind(("0.0.0.0", 1234))

0.0。 0.0 表示本地计算机上的所有 IPv4 地址

您似乎在请求休息而不是通过套接字连接到服务器。我不认为 alamoFire 是为处理套接字而构建的。试试这个。

https://github.com/daltoniam/Starscream