我在家庭网络中的覆盆子PI上运行pytHon服务器,设置简单。在我的电脑上我有一个基本的HTML页面,有一些Javascript tHat s可以与我的覆盆子pi上的py服务器进行通信。服务器上的覆盆子pi工作完美wHen从我的PC上的telnet测试。
这是PytHon代码:
import socket
from time import*
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ""
port = 51025
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print "Connection open"
print conn, addr
while 1:
data = conn.recv(4096)
if data:
print "msg received"
conn.sendall(data +" 12 uniflu")
else:
print "connection lost"
s.listen(1)
conn, addr = s.accept()
print "Connection open"
print conn, addr
这是javascript代码:
window.onload = function() {
// Get references to elements on the page.
var form = document.getElementById('message-form');
var messageField = document.getElementById('message');
var messagesList = document.getElementById('messages');
var socketStatus = document.getElementById('status');
var closeBtn = document.getElementById('close');
var openBtn = document.getElementById('open');
// The rest of the code in this tutorial will go here...
// Create a new WebSocket.
var socket = new WebSocket('ws://192.168.0.81:51025');
// Close the WebSocket connection when the close button is clicked.
// Show a connected message when the WebSocket is opened.
socket.onopen = function(event) {
socketStatus.innerHTML = 'Connected to: ' + event.currentTarget.URL;
socketStatus.className = 'open';
};
// Handle any errors that occur.
socket.onerror = function(error) {
console.log('WebSocket Error: ' + error);
};
// Close the WebSocket connection when the close button is clicked.
closeBtn.onclick = function(e) {
e.preventDefault();
// Close the WebSocket.
socket.close();
return false;
};
// Show a disconnected message when the WebSocket is closed.
socket.onclose = function (event) {
var reason;
alert(event.code);
// See http://tools.ietf.org/html/rfc6455#section-7.4.1
if (event.code == 1000)
console.log( "Normal closure, meaning that the purpose for which the connection was established has been fulfilled.");
else if(event.code == 1001)
console.log("An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page.");
else if(event.code == 1002)
console.log("An endpoint is terminating the connection due to a protocol error");
else if(event.code == 1003)
console.log("An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).");
else if(event.code == 1004)
console.log("Reserved. The specific meaning might be defined in the future.");
else if(event.code == 1005)
console.log("No status code was actually present.");
else if(event.code == 1006)
console.log("The connection was closed abnormally, e.g., without sending or receiving a Close control frame");
else if(event.code == 1007)
console.log("An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [http://tools.ietf.org/html/rfc3629] data within a text message).");
else if(event.code == 1008)
console.log("An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other sutible reason, or if there is a need to hide specific details about the policy.");
else if(event.code == 1009)
console.log("An endpoint is terminating the connection because it has received a message that is too big for it to process.");
else if(event.code == 1010) // Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
console.log("An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " + event.reason);
else if(event.code == 1011)
console.log("A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.");
else if(event.code == 1015)
console.log("The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).");
else
console.log("Unknown reason");
$("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "The connection was closed for reason: " + reason);
};
// Send a message when the form is submitted.
form.onsubmit = function(e) {
e.preventDefault();
// Retrieve the message from the textarea.
var message = messageField.value;
// Send the message through the WebSocket.
socket.send(message);
// Add the message to the messages list.
messagesList.innerHTML += '<li class="sent"><span>Sent:</span>' + message +
'</li>';
// Clear out the message field.
messageField.value = '';
return false;
};
// Handle messages sent by the server.
socket.onmessage = function(event) {
var message = event.data;
messagesList.innerHTML += '<li class="received"><span>Received:</span>' +
message + '</li>';
};
};
当我在我的firefox浏览器中加载HTML页面时,我可以看到通过打印输出在pytHon中建立连接,但是在服务器变得没有响应之后。我还在浏览器中看到错误消息1006。有谁知道他的区域,可以帮助我吗?它是什么从putty而不是从Web浏览器中运行得很好?