websocket-client:socket.error:[Errno 104]通过对等方重置连接

时间:2018-03-22 13:33:52

标签: django websocket django-channels

我正在使用 Django频道 来构建实时聊天应用。我已经安装了websocket-client,当我尝试使用ws.connect()进行连接时,它会抛出错误104.

  

通过对等方重置连接。

它首先进行握手,但会立即断开连接。

我的 routing.py 文件:

from channels.routing import route
from channels import Group


def ws_connect(message):
  print 'ssssssssssssss'
  Group('users').add(message.reply_channel)
  message.reply_channel.send({"accept": True})


def ws_disconnect(message):
  Group('users').discard(message.reply_channel)


def message_handler(message):
    print(message['text'])

channel_routing = [
    route("websocket.receive", message_handler),
    route('websocket.connect', ws_connect),
    route('websocket.disconnect', ws_disconnect),
]

putty 并行运行:

root@in:/home/django/django_project# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 22, 2018 - 13:24:20
Django version 1.11.10, using settings 'django_project.settings'
Starting Channels development server at http://127.0.0.1:8000/
Channel layer default (asgi_redis.core.RedisChannelLayer)
Quit the server with CONTROL-C.
2018-03-22 13:24:20,346 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-03-22 13:24:20,348 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-03-22 13:24:20,348 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-03-22 13:24:20,350 - INFO - server - HTTP/2 support enabled
2018-03-22 13:24:20,350 - INFO - server - Using native Twisted mode on channel layer
2018-03-22 13:24:20,351 - INFO - server - Listening on endpoint tcp:port=8000:interface=127.0.0.1
2018-03-22 13:24:20,353 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
ssssssssssssss
[2018/03/22 13:24:26] WebSocket HANDSHAKING / [127.0.0.1:38966]
[2018/03/22 13:24:31] WebSocket DISCONNECT / [127.0.0.1:38966]

我已安装 websocket-client 并且:

>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://localhost:8000")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/websocket/_core.py", line 220, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_handshake.py", line 69, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_handshake.py", line 133, in _get_resp_headers
    status, resp_headers, status_message = read_headers(sock)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_http.py", line 236, in read_headers
    line = recv_line(sock)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_socket.py", line 102, in recv_line
    c = recv(sock, 1)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_socket.py", line 81, in recv
    bytes_ = sock.recv(bufsize)
socket.error: [Errno 104] Connection reset by peer

0 个答案:

没有答案