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

使用Alamofire连接到本地主机服务器(python),python,ios,swift,server,localhost,Python,Ios,Swift,Server,Localhost,因此,我试图通过简单的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. client

因此,我试图通过简单的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-来自网络首选项(mac)的ip 我还连接到同一个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地址

似乎您正在处理rest请求,而不是通过套接字连接到服务器。我不认为alamoFire是用来处理套接字的。试试这个


您遇到了什么alamofire错误?@EricHua Code=-1004“无法连接到服务器。”