gpt4 book ai didi

python - 让 iOS 应用程序与我的家庭服务器对话

转载 作者:行者123 更新时间:2023-11-29 13:17:04 25 4
gpt4 key购买 nike

目前,我的衣橱里有一台家庭服务器,基本上什么都不做。它安装了 Ubuntu Server 8.0.4,稍后将使用用于 Web 开发的 apache,安装了 ssh 和 python/twisted。

问题是:

我创建了一个应用程序来与“本地主机”端口 40 通信,使用套接字实现,这是我所做的但我想开发的链接:http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

现在连接到本地主机没有问题,但我想扩展它以与我的服务器一起使用。

我在我的服务器上实现了 python 协议(protocol),并更改了我在 iOS 应用程序中访问的 ip 地址。这是我的实现,除了我正在访问的端口外,它与教程完全相同。

from twisted.internet.protocol import Factory, Protocol

from twisted.internet import reactor

class IphoneChat(Protocol):

def connectionMade(self):
self.factory.clients.append(self)
print "clients are ", self.factory.clients

def connectionLost(self, reason):
self.factory.clients.remove(self)

def dataReceived(self, data):
a = data.split(':')
print a
#b = password.split(':')
#print b
if len(a) > 1:
command = a[0]
content = a[1]
#username = a[2]
#password = a[2]

msg = ""
#msg2 = ""
if command == "username":
self.name = data
msg = self.name + " has joined"
#self.name = password
#msg2 = self.name + " this is his password"
#print msg

elif command == "password":
self.name = data
msg = self.name + " this is their password"

elif command == "msg":
msg = self.name + ": " + data
print msg
#msg2 = self.name + ": " + password
#print msg2

for c in self.factory.clients:
c.message(msg)#1, msg2)

def message(self, message):
self.transport.write(message + '\n')


factory = Factory()

factory.protocol = IphoneChat #here1#

factory.clients = []

reactor.listenTCP(40, factory)

print "Iphone Chat server started"

reactor.run()

所以真正的问题是,我无法将客户端连接到我的服务器或其他东西......抱歉,我对网络还很陌生。

任何意见都会有所帮助。

最佳答案

如果您还没有建立从 Internet 到您的服务器的网络通信,那么在您想要建立您可以先用简单的测试用例做到这一点之前,有很多方法可以阻止来自 Internet 的流量到达您的程序服务器。

您的服务器是否通过路由器连接到互联网?如果是这样,您可以使用 netcattelnet 之类的东西从本地网络内部与服务器通信(即使用 192.168.xxx.xxx ip 地址)吗?

如果可行,您应该从网络外部尝试(即使用来自 whatismyip.net 或类似地址的其他 IP 地址)。如果您之前真的没有网络经验,您可能会忽略设置端口转发,这是您路由器上的一项设置。

有很多教程教你如何设置 Ubuntu 家庭服务器,我建议学习如何托管一个(非常)简单的网页作为学习如何联网的一种方式,这将有助于调试一个像您正在制作的网络程序。

关于python - 让 iOS 应用程序与我的家庭服务器对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15474356/

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