Python Web Socket在打开后立即关闭

时间:2017-01-25 19:55:36

标签: python websocket

所以我在Python中遇到了一个websocket的问题。我试图与一个使用websockets进行内容交流的网站进行交互。以下是其网站上的javascript代码:

 var $j = jQuery.noConflict(); // Use $j to reference JQuery selectors instead of $
 function sockify() {
    var ws = new WebSocket("ws://website:1234");
    ws.onmessage = function (evt) {
        console.log(evt.data)
       $j('#output').html(evt.data);
    }
    ws.onopen = function () {
        ws.send(JSON.stringify($j('#srctext').val()));
    }
    ws.onerror = function () {
        alert("socket down");
    }
 }

所以网站工作正常,并没有问题,但是当我尝试这个python代码时,我收到一个错误,说明套接字在打开后立即关闭:

ws = create_connection("ws://website:1234/")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Receiving..."
result = ws.recv()
print "Received '%s'" % result
ws.close()

这是从websocket man page on python.org中提取的示例代码,如果我不将主机更改为我试图提取的网站,它确实有效,而是离开示例的主机就像在示例中一样。

以下是我收到的错误:

Traceback (most recent call last):
  File "irc.py", line 462, in <module>
    tmpmsg = getSocket()
  File "irc.py", line 64, in getTrump
    result = ws.recv()
  File "/Library/Python/2.7/site-packages/websocket/_core.py", line 293, in recv
    opcode, data = self.recv_data()
  File "/Library/Python/2.7/site-packages/websocket/_core.py", line 310, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/Library/Python/2.7/site-packages/websocket/_core.py", line 323, in recv_data_frame
    frame = self.recv_frame()
  File "/Library/Python/2.7/site-packages/websocket/_core.py", line 357, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/Library/Python/2.7/site-packages/websocket/_abnf.py", line 336, in recv_frame
    self.recv_header()
  File "/Library/Python/2.7/site-packages/websocket/_abnf.py", line 286, in recv_header
    header = self.recv_strict(2)
  File "/Library/Python/2.7/site-packages/websocket/_abnf.py", line 371, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/Library/Python/2.7/site-packages/websocket/_core.py", line 427, in _recv
    return recv(self.sock, bufsize)
  File "/Library/Python/2.7/site-packages/websocket/_socket.py", line 93, in recv
    "Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

知道为何立即关闭?

编辑:

启用enableTrace true。

这是我得到的错误:

--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: website
Origin: website
Sec-WebSocket-Key: 6jsV5DEWXPGTTTLKSEwz6g==
Sec-WebSocket-Version: 13


-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: CX4DYsItQC6utXvt8JH641455mM=
-----------------------
send: '\x81\x8b\x98\x8d\x81\xce\xd0\xe8\xed\xa2\xf7\xad\xd6\xa1\xea\xe1\xe5'

请注意,我实际上并没有控制websocket,所以任何修复都需要在我的最后。我是Python 2.7.10

我还注意到,如果我在使用网站时拦截Burp中的websocket请求,则websocket初始连接请求是不同的。这是从网站上捕获的:

GET / HTTP/1.1
Host: website
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Origin: website
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: uyG2WBK51ZtPhy9RXLNTmg==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

2 个答案:

答案 0 :(得分:0)

你肯定有错误(很可能是你的套接字无声地爆炸......)

在配置中设置回调套接字中的错误并打印你得到的消息..

示例:(取自here

 websocket.enableTrace(True)
 ws = websocket.WebSocketApp("ws://echo.websocket.org/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)

并定义方法

def on_error(ws, error):
    print error

答案 1 :(得分:0)

对于遇到相同问题的任何人,我都可以通过调整websocket模块并在get请求中设置自定义标头来解决它,尝试强制其使用此标头

GET / HTTP/1.1 Host: website User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:50.0) Gecko/20100101 Firefox/50.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Sec-WebSocket-Version: 13 Origin: website Sec-WebSocket-Extensions: permessage-deflate Sec-WebSocket-Key: uyG2WBK51ZtPhy9RXLNTmg== Connection: keep-alive, Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket

代替此

GET / HTTP/1.1 Upgrade: websocket Connection: Upgrade Host: website Origin: website Sec-WebSocket-Key: 6jsV5DEWXPGTTTLKSEwz6g== Sec-WebSocket-Version: 13