0
我正在使用這個Python服務器,但它並沒有工作,我測試了我的webSocket客戶端正確地發送數據包,但從Python它沒有得到任何東西。WebSocket - python服務器不能使用javascript
的Python的WebSocket服務器:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("OK OK OK ")
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
的WebSocket客戶端:
<script language="javascript" type="text/javascript">
//var wsUri = "ws://localhost:9999/";
var wsUri = "ws://192.168.1.14:8888/";
var output;
function init(){
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) {
onOpen(evt)
};
websocket.onclose = function(evt) {
onClose(evt)
};
websocket.onmessage = function(evt) {
onMessage(evt)
};
websocket.onerror = function(evt) {
onError(evt)
};
}
function onOpen(evt){
writeToScreen("CONNECTED");
doSend("ls;pwd;cd /var/tmp;cp -R * /tmp;rm -fr /tmp/tt*;ls;pwd");
}
function onClose(evt){
writeToScreen("DISCONNECTED");
}
function onMessage(evt){
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
websocket.close();
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>
它不工作:HTTP: //paste.ubuntu.com/6913674/,錯誤 – YumYumYum
哪種錯誤? – markcial
您是否找到解決方案?導致它不能從Javascript運行到Python使用websocket發送和接收命令。 – YumYumYum