火狐开发者控制台(F12) belov中的连接错误消息。如何从pyscript客户端连接Elastic。我简单地连接Pyscript CDN和导入python()中的弹性搜索库。
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
- from elasticsearch import Elasticsearch
<html>
<head>
<title>Matplotlib</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- from elasticsearch import Elasticsearch
</py-env>
</head>
<body>
<py-script>
# Password for the 'elastic' user generated by Elasticsearch
ELASTIC_PASSWORD = "LKQp5hTjR6KC5zTbFtHT2V9r"
# Found in the 'Manage Deployment' page
CLOUD_ID = " "
# Create the client instance
client = Elasticsearch(
cloud_id=CLOUD_ID,
basic_auth=("elastic", ELASTIC_PASSWORD)
)
# Successful response!
client.info()
# {'name': 'instance-0000000000', 'cluster_name': ...}
</py-script>
</body>
</html> 错误
Uncaught (in promise) PythonError: Traceback (most recent call last):
pyparsing.exceptions.ParseException: Expected string_end, found 'elasticsearch' (at char 5), (line:1, col:6)
raise InvalidRequirement(
packaging.requirements.InvalidRequirement: Parse error at "'elastics'": Expected string_end
setTimeout handler*hiwire_call_bound pyodide.asm.js:14
callPyObjectKwargs pyproxy.gen.ts:360
callPyObject pyproxy.gen.ts:384
wrapper pyodide.asm.js:14
setTimeout handler*hiwire_call_bound pyodide.asm.js:14发布于 2022-06-03 19:19:11
您的<py-env>声明是不正确的。它应该是这样的:
<py-env>
elasticsearch
</py-env>像这样进口弹弓:
<py-script>
from elasticsearch import Elasticsearch
...然而,弹性搜索并不是一个支持的软件包。它依赖于由浏览器支持的Python和Requests包。Requests包使用操作系统TCP。该API在浏览器中不可用。这不是PyScript限制,而是所有应用程序在浏览器中的安全限制。
唯一的解决方案是修改弹性搜索以使用浏览器支持的API(如取API )。
https://stackoverflow.com/questions/72490512
复制相似问题