【问题标题】:Android connecting to a Python ServerAndroid 连接到 Python 服务器
【发布时间】:2013-01-11 06:58:12
【问题描述】:

我的朋友用 Python 制作了一个服务器,并希望我创建一个连接到它的 Android 应用程序,但我不知道从哪里开始。这是服务器的代码。谁能帮助我或让我走上正确的道路?

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
        if len(a) > 1:
            command = a[0]
            content = a[1]

            msg = ""
            if command == "iam":
                self.name = content
                msg = self.name + " has joined"

            elif command == "msg":
                msg = self.name + ": " + content

            print msg

            for c in self.factory.clients:
                c.message(msg)

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

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

reactor.listenTCP(5633, factory)
print "Chat Server Started..."
reactor.run()

【问题讨论】:

标签: android python tcp


【解决方案1】:

如果您是网络编程的新手,阅读client-server modelsocketsBerkeley's in particular 是一个好的开始。

掌握了基本概念后,你会想检查Android socket documentation,最后试试a tutorial

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2012-11-27
    • 2021-08-12
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多