Python 使用线程和扭曲的sendMessage的高速公路
通过这个线程()我试图让下面的代码正常工作 但我得到了以下错误:Python 使用线程和扭曲的sendMessage的高速公路,python,websocket,twisted,autobahn,Python,Websocket,Twisted,Autobahn,通过这个线程()我试图让下面的代码正常工作 但我得到了以下错误: File "/usr/local/lib/python2.7/dist-packages/autobahn/websocket/protocol.py", line 2421, in sendMessage assert(type(payload) == bytes) exceptions.AssertionError: 谁能告诉我哪里出了问题 from autobahn.twisted.websocket imp
File "/usr/local/lib/python2.7/dist-packages/autobahn/websocket/protocol.py", line 2421, in sendMessage
assert(type(payload) == bytes)
exceptions.AssertionError:
谁能告诉我哪里出了问题
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
import threading
from twisted.python import log
from twisted.internet import reactor
import sys
import time
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
class Connection(threading.Thread):
def __init__(self):
super(Connection, self).__init__()
self.factory=WebSocketServerFactory("ws://localhost:9000", debug=False)
def run(self):
log.startLogging(sys.stdout)
self.factory.protocol = MyServerProtocol()
reactor.listenTCP(9000, self.factory)
reactor.run(installSignalHandlers=0)
def send(self, data):
data = format(data.encode('utf8'))
protocol = MyServerProtocol()
reactor.callFromThread(protocol.sendMessage, protocol, data)
connection = Connection()
connection.daemon = True
connection.start()
print "Running"
for x in range(1, 100):
connection.send("hello")
time.sleep(2)
谢谢
编辑1:
完全错误:
2015-04-30 07:27:09+0930 [-] Log opened.
2015-04-30 07:27:09+0930 [-] WebSocketServerFactory starting on 9000
2015-04-30 07:27:09+0930 [-] Running
2015-04-30 07:27:09+0930 [-] Starting factory <autobahn.twisted.websocket.WebSocketServerFactory object at 0x7fa97f888a50>
2015-04-30 07:27:09+0930 [-] Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/home/me/Development/eclipse/new_workspace/Engine2/websockets/maybe.py", line 39, in run
reactor.run(installSignalHandlers=0)
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/base.py", line 1192, in run
self.mainLoop()
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/base.py", line 1201, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/base.py", line 797, in runUntilCurrent
f(*a, **kw)
File "/usr/local/lib/python2.7/dist-packages/autobahn/websocket/protocol.py", line 2421, in sendMessage
assert(type(payload) == bytes)
exceptions.AssertionError:
在twisted中不能这样开始线程
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from twisted.python import log
from twisted.internet import reactor
import sys
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
def send(self, data):
data = format(data.encode('utf8'))
protocol = MyServerProtocol()
reactor.callFromThread(protocol.sendMessage, protocol, data)
class Test(MyServerProtocol):
def __init__(self):
self.factory=WebSocketServerFactory("ws://localhost:9000", debug=False)
log.startLogging(sys.stdout)
self.factory.protocol = MyServerProtocol
reactor.listenTCP(9000, self.factory)
reactor.run(installSignalHandlers=0)
Test = Test()
print "Running"
反应器必须在主线程中启动,要在twisted中使用thread,您必须使用twisted threading模块,但为什么要使用thread?这解释了我试图做的事情:如果是向twisted发送消息的外部应用程序,那么为什么要使用类连接(threading.thread):在主线程和时间中直接使用reactor。sleep()无法应用。使用您的代码我制作了一个没有线程的新版本,但进程永远无法到达:Test.send(“hello”)您正在创建一个服务器,因此无法向尚不存在的客户端发送消息,因此请删除Test.send(“hello”)
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from twisted.python import log
from twisted.internet import reactor
import sys
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
def send(self, data):
data = format(data.encode('utf8'))
protocol = MyServerProtocol()
reactor.callFromThread(protocol.sendMessage, protocol, data)
class Test(MyServerProtocol):
def __init__(self):
self.factory=WebSocketServerFactory("ws://localhost:9000", debug=False)
log.startLogging(sys.stdout)
self.factory.protocol = MyServerProtocol
reactor.listenTCP(9000, self.factory)
reactor.run(installSignalHandlers=0)
Test = Test()
print "Running"