这是关于Websocket Protocol 76中握手的问题。
我已经编写了一个客户端和服务器,但是无法获取客户端 接受握手。我可以看到它被返回,但客户端立即关闭连接。我猜我的md5sum响应必须是错误的。
据我所知,我正在遵循正确的程序,有谁能告诉我我做错了什么?
def create_handshake_resp(handshake):
# parse request
final_line = ""
lines = handshake.splitlines()
for line in lines:
parts = line.partition(":")
if parts[0] == "Sec-WebSocket-Key1":
key1 = parts[2]
elif parts[0] == "Sec-WebSocket-Key2":
key2 = parts[2]
final_line = line
#concat the keys and encrypt
e = hashlib.md5()
e.update(parse_key(key1))
e.update(parse_key(key2))
e.update(final_line)
return "HTTP/1.1 101 WebSocket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nWebSocket-Origin: http://%s\r\nWebSocket-Location: ws://%s/\r\nWebSocket-Protocol: sample\r\n\r\n%s" % (httphost, sockethost, e.digest())
def parse_key(key):
spaces = -1
digits = ""
for c in key:
if c == " ":
spaces += 1
if is_number(c):
digits = digits + c
new_key = int(digits) / spaces
return str(new_key)
正如你所看到的,我正在执行我认为对键的正确操作(按空格数,concat结果和请求的最后一行除以MD5),并且16字节响应肯定是返回。
我们非常感谢任何帮助,只要我有工作副本,我就会在这里发布。
感谢。
编辑:
更改标题以符合kanaka的回复。握手仍未被客户接受。 我发现了如何在Chromium中显示请求,这是请求和响应:
(P) t=1291739663323 [st=3101] WEB_SOCKET_SEND_REQUEST_HEADERS
--> GET / HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: ---
Origin: http://---
Sec-WebSocket-Key1: 3E 203C 220 642;
Sec-WebSocket-Key2: Lg 590 ~5 703O G7 =%t 9
\x74\x66\xef\xab\x50\x60\x35\xc6\x0a
(P) t=1291739663324 [st=3102] SOCKET_STREAM_SENT
(P) t=1291739663348 [st=3126] SOCKET_STREAM_RECEIVED
(P) t=1291739663348 [st=3126] WEB_SOCKET_READ_RESPONSE_HEADERS
--> HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://---
Sec-WebSocket-Location: ws://---/
Sec-WebSocket-Protocol: sample
\xe7\x6f\xb9\xcf\xae\x70\x57\x43\xc6\x20\x85\xe7\x39\x2e\x83\xec\x0
广告逐字逐句,除非我出于显而易见的原因删除了IP地址。
答案 0 :(得分:5)
你有几个问题立刻突然出现在我身上:
以下是我如何计算wsproxy(noVNC HTML5 VNC客户端的一部分)中的响应chksum:
import struct, md5
...
spaces1 = key1.count(" ")
spaces2 = key2.count(" ")
num1 = int("".join([c for c in key1 if c.isdigit()])) / spaces1
num2 = int("".join([c for c in key2 if c.isdigit()])) / spaces2
return md5(struct.pack('>II8s', num1, num2, key3)).digest()
答案 1 :(得分:2)
以下是WebSocket客户端/服务器(Javascript中的客户端,Python 2.6中的服务器)的工作示例
它使用了各个地方的例子(包括kanaka的答案/ noVNC,以及this page和this page)
适用于iOS 4.3上的Chrome 10.0.648.127,Safari 5.0.3和iPad上的MobileSafari
这绝不是编写良好的代码(示例HTML页面特别糟糕) - 使用风险等等......
#!/usr/bin/env python
import socket
import threading
import struct
import hashlib
PORT = 9876
def create_handshake_resp(handshake):
final_line = ""
lines = handshake.splitlines()
for line in lines:
parts = line.partition(": ")
if parts[0] == "Sec-WebSocket-Key1":
key1 = parts[2]
elif parts[0] == "Sec-WebSocket-Key2":
key2 = parts[2]
elif parts[0] == "Host":
host = parts[2]
elif parts[0] == "Origin":
origin = parts[2]
final_line = line
spaces1 = key1.count(" ")
spaces2 = key2.count(" ")
num1 = int("".join([c for c in key1 if c.isdigit()])) / spaces1
num2 = int("".join([c for c in key2 if c.isdigit()])) / spaces2
token = hashlib.md5(struct.pack('>II8s', num1, num2, final_line)).digest()
return (
"HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
"Upgrade: WebSocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Origin: %s\r\n"
"Sec-WebSocket-Location: ws://%s/\r\n"
"\r\n"
"%s") % (
origin, host, token)
def handle(s, addr):
data = s.recv(1024)
s.send(create_handshake_resp(data))
lock = threading.Lock()
while 1:
print "Waiting for data from", s, addr
data = s.recv(1024)
print "Done"
if not data:
print "No data"
break
print 'Data from', addr, ':', data
# Broadcast received data to all clients
lock.acquire()
[conn.send(data) for conn in clients]
lock.release()
print 'Client closed:', addr
lock.acquire()
clients.remove(s)
lock.release()
s.close()
def start_server():
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', PORT))
s.listen(1)
while 1:
conn, addr = s.accept()
print 'Connected by', addr
clients.append(conn)
threading.Thread(target = handle, args = (conn, addr)).start()
clients = []
start_server()
另外,一个糟糕的示例HTML页面显示它正常工作:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script type="application/javascript">
var ws;
function init() {
var servermsg = document.getElementById("servermsg");
ws = new WebSocket("ws://localhost:9876/");
ws.onopen = function(){
servermsg.innerHTML = servermsg.innerHTML + "<br>Server connected";
servermsg.innerHTML = servermsg.innerHTML + "<br>Sending message to server";
ws.send("Hello Mr. Server!");
};
ws.onmessage = function(e){
servermsg.innerHTML = servermsg.innerHTML + "<br>Recieved data: " + e.data;
};
ws.onclose = function(){
console.log("Server disconnected");
servermsg.innerHTML = servermsg.innerHTML + "<br>Connected";
};
}
function postmsg(){
var text = document.getElementById("message").value;
ws.send(text);
servermsg.innerHTML = servermsg.innerHTML + "<br>Sent: " + text;
return false;
}
</script>
</head>
<body onload="init();">
<form action="" onSubmit="postmsg()">
<input type="text" name="message" value="" id="message">
<input type="submit" name="submit" value="" id="submit">
</form>
<div id="servermsg"><h1>Message log:</h1></div>
</body>
</html>