WebSocketBadStatusException:握手状态403禁止-+-+-

8iwquhpp  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(290)

我想在Android应用程序上调试H5页面,但当我使用websocket连接webSocketDebuggerUrl时,它报告错误WebSocketBadStatusException:握手状态403禁止-+-+-。请问如何解决这个错误?
1.将Android手机连接到我的Windows电脑。
1.在手机上启用开发者模式。
1.在Chrome浏览器中输入Chrome://inspect,它会显示webview页面。键入inspect链接,它会打开一个devtools并正确显示webview页面。
1.使用命令adb forward tcp:9222 localabstract::webview_devtools_remote_17180将套接字进程转发到计算机。
1.在Chrome浏览器中输入http://localhost:9225/json,它会显示一个列表。
1.连接列表中的webSocketDebuggerUrl

import websocket
ws = websocket.WebSocket()
ws.connect("ws://localhost:9225/devtools/page/625C5EB348BF3D8E0B359614B7003DE3")

1.它报告错误

>>> ws.connect("ws://localhost:9225/devtools/page/625C5EB348BF3D8E0B359614B7003DE3")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\work\MiniConda\lib\site-packages\websocket\_core.py", line 253, in connect
    self.handshake_response = handshake(self.sock, url, *addrs, **options)
  File "D:\work\MiniConda\lib\site-packages\websocket\_handshake.py", line 57, in handshake
    status, resp = _get_resp_headers(sock)
  File "D:\work\MiniConda\lib\site-packages\websocket\_handshake.py", line 150, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status {status} {message} -+-+- {headers} -+-+- {body}".format(status=status, message=status_message, headers=resp_headers, body=response_body), status, status_message, resp_headers, response_body)
websocket._exceptions.WebSocketBadStatusException: Handshake status 403 Forbidden -+-+- {'content-length': '239', 'content-type': 'text/html'} -+-+- b'Rejected an incoming WebSocket connection from the http://localhost:9224 origin. Use the command line flag --remote-allow-origins=http://localhost:9224 to allow connections from this origin or --remote-allow-origins=* to allow all origins.'

根据错误信息,它似乎与调试webview的权限有关,但我不确定。

ifmq2ha2

ifmq2ha21#

要解决这个问题,你需要添加标志--remote-allow-origins=http://127.0.0.1:{port_browser}当你去启动浏览器

options = [f'--remote-debugging-port={self.port}', f'--remote-allow-origins=http://127.0.0.1:{self.port}']
if headless is True:
    options.append('--headless')
self._browser = subprocess.Popen([browser_path] + options)

相关问题