因此,我需要制作一个可以放在计算机上的python应用程序,打开通往运行flask-socketio的服务器的websocket管道,然后将数据跨管道传输到服务器。该数据将是json数据,现在我正在使用字符串进行测试。
我在使用python中的“ websockets”库连接到我正在制作的“ flask-socketio”后端时遇到问题。
from flask import Flask, render_template, request, url_for,
copy_current_request_context
from flask_socketio import SocketIO, emit
import logging
logging.basicConfig()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
socketio = SocketIO(app)
@socketio.on('connect', namespace='/')
def connect():
print('Client connected')
@socketio.on('disconnect', namespace='/')
def disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app, debug=True)
import asyncio
import websockets
async def hello():
async with websockets.connect(
'ws://127.0.0.1:5000') as websocket:
name = 'joe' # input("What's your name? ")
await websocket.send('message', name)
print(f"> {name}")
greeting = await websocket.recv()
print(f"< {greeting}")
asyncio.get_event_loop().run_until_complete(hello())
print("end")
当我使用客户端1时,我将其作为客户端进程的输出:
Traceback (most recent call last):
File "C:/project_files/aWebUAS/micro-services/New-Capabilities-Investigation/web_socket_client.py", line 15, in <module>
asyncio.get_event_loop().run_until_complete(hello())
File "C:\Program Files\Anaconda3\lib\asyncio\base_events.py", line 466, in run_until_complete
return future.result()
File "C:/project_files/aWebUAS/micro-services/New-Capabilities-Investigation/web_socket_client.py", line 6, in hello
'ws://127.0.0.1:5000') as websocket:
File "C:\Program Files\Anaconda3\lib\site-packages\websockets\py35\client.py", line 2, in __aenter__
return await self
File "C:\Program Files\Anaconda3\lib\site-packages\websockets\py35\client.py", line 20, in __await_impl__
extra_headers=protocol.extra_headers,
File "C:\Program Files\Anaconda3\lib\site-packages\websockets\client.py", line 286, in handshake
raise InvalidStatusCode(status_code)
websockets.exceptions.InvalidStatusCode: Status code not 101: 404
在烧瓶过程中,我看到:
127.0.0.1 - - [2019-04-05 09:17:13] "GET / HTTP/1.1" 404 342 0.000999
import websocket
ws = websocket.WebSocket()
ws.connect("ws://127.0.0.1:5000")
ws.send("Hello, World")
在客户端2进程上,我将其作为输出:
Traceback (most recent call last):
File "C:/project_files/aWebUAS/micro-services/New-Capabilities-Investigation/web_socket_client_2.py", line 3, in <module>
ws.connect("ws://127.0.0.1:5000")
File "C:\Program Files\Anaconda3\lib\site-packages\websocket\_core.py", line 226, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "C:\Program Files\Anaconda3\lib\site-packages\websocket\_handshake.py", line 79, in handshake
status, resp = _get_resp_headers(sock)
File "C:\Program Files\Anaconda3\lib\site-packages\websocket\_handshake.py", line 160, in _get_resp_headers
raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 404 NOT FOUND
并且烧瓶过程显示如下:
127.0.0.1 - - [2019-04-05 09:11:57] "GET / HTTP/1.1" 404 342 0.001000
一些阅读资料告诉我,我没有在烧瓶中正确敲打我的socketio处理程序,但是我不确定该如何定位。
非常感谢您的帮助。我的目标是让python脚本与flask socketio服务器通信。我对此没有任何看法,但是希望坚持使用烧瓶作为服务器。
TIA-伊恩
答案 0 :(得分:0)
您现在可能已经弄清楚了,但是问题很可能是flask-socketio是Socket.IO实现,而不是websocket实现。 Socket.IO使用websockets作为其通信协议之一,但是您将需要一个Socket.IO客户端才能连接到flask-socketio服务器。
请参见socketio's docs,具体是:
Socket.IO不是WebSocket实现。虽然确实是Socket.IO 在可能的情况下使用WebSocket作为传输方式,它将一些元数据添加到 每个数据包:当一个数据包时,数据包类型,名称空间和ack ID 需要消息确认。这就是为什么WebSocket客户端会 无法成功连接到Socket.IO服务器,并且 Socket.IO客户端将无法连接到WebSocket服务器 要么。
看看这个python Socket.IO客户端。 https://github.com/miguelgrinberg/python-socketio