Программа написана для связи между HTML и Python Server.Соединение установлено, но коммуникационное сообщение, которое я написал в HTML (в формате строки), не отображается на стороне сервера.
Я пробовал программы на Python-сервере, веб-сокеты и программирование HTML
'''Серверная программа, использующая python' ''
def RcvDatafromUI(self):
while True:
json_data = self.conn.recv(1024)
try:
if json_data == b'\r\n' or b'':
print("Looping Again")
continue
else:
print("RcvdDAta : ", json_data)
json_string = json_data.decode('utf-8')
print("DecodedDAta : ", json_string)
#print('type decode=',type(json_string))
json_dict = json.loads(ast.literal_eval(json_string))
print('json_dict=', json_dict)
y = parsejsondata.ParseJson(json_dict)
if y == "Success":
PloverMain.SendDAtatoUI('{"Command":"Start
","Status":"Success/Fail"}')
PloverMain.RcvDatafromUI()
'' 'html code' ''
<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
function WebSocketTest() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var exampleSocket = new WebSocket("ws://localhost:12345/echo");
alert("website opened")
exampleSocket.onopen = function(event) {
// Web Socket is connected, send data using send()
exampleSocket.send('{"Command":"Start","Status":"Check"}');
alert("Message is sent...");
};
exampleSocket.onmessage = function (event) {
var received_msg = event.data;
alert("Message is received...");
};
exampleSocket.onclose = function() {
// websocket is closed.
alert("Connection is closed...");
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id = "sse">
<a href = "javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
Expected:(on server side)
RcvdDAta : '{"Command":"Start","Status":"Check"}'
Acutal:
RcvdDAta :
b'GET /echo HTTP/1.1\r\nHost: localhost:12345\r\nConnection: Upgrade\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\nOrigin: file://\r\nSec-WebSocket-Version: 13\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: en-US,en;q=0.9\r\nSec-WebSocket-Key: s1qAa96xxm9IZqU21C0TQA==\r\nSec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n'