【发布时间】:2015-01-21 10:34:54
【问题描述】:
我需要在我的 Python 服务器中实现 Websocket 握手。我的 python 服务器使用 Twisted 进行事件处理。我发现 this webpage 解释了这个过程,但是当涉及到这一点时,我真的很头疼。那么如何在下面的服务器代码中实现 Websocket 握手:(注意,我已经从服务器中取出了我所有的项目特定代码,以便于阅读)
import os
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
#self.factory.clients.append(self)
print "A new client has connected"
def connectionLost(self, reason):
for c in self.factory.clients:
if c == self:
self.factory.client.remove(self)
print "client disconnected"
def dataReceived(self, data):
#print "Message Received: ", data
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
port = 3000
print "Server started: "
print port
reactor.listenTCP(port, factory)
reactor.run()
【问题讨论】:
标签: python sockets websocket twisted