问题描述
我正在尝试使用cefpython创建一个混合的python-js应用程序.
I'm trying to make a hybrid python-js application with cefpython.
我想要:
- cef python应用程序本地的JS和HTML文件(例如,在'./html'、'./js'等中)
- 加载其中一个HTML文件作为初始页面
- 避免文件相互访问(例如,目录之间)带来的任何CORS问题
以下内容似乎可以加载第一页:
The following seems to work to load the first page:
browser = cef.CreateBrowserSync(url='file:///html/index.html',
window_title="Rulr 2.0")
但是,然后我遇到了CORS问题.我还需要运行网络服务器吗?还是有一种有效的本地文件处理模式?
However, I then hit CORS issues.Do I need to run a webserver also? Or is there an effective pattern for working with local files?
推荐答案
尝试将"disable-web-security"开关传递给cef.初始化或设置BrowserSettings.web_security_disabled.
Try passing "disable-web-security" switch to cef.Initialize or set BrowserSettings.web_security_disabled.
还尝试设置BrowserSettings.file_access_from_file_urls_allowed和BrowserSettings.universal_access_from_file_urls_allowed.
Try also setting BrowserSettings.file_access_from_file_urls_allowed and BrowserSettings.universal_access_from_file_urls_allowed.
CEF中有一些用于加载自定义内容的选项,这些选项可用于加载文件系统内容,而没有任何安全限制.有一个资源处理程序,一个方案处理程序和一个资源管理器.在CEF Python中,当前仅资源处理程序可用.在README-Examples.md页面上有wxpython-response.py示例.
There are a few options in CEF for loading custom content and that can be used to load filesystem content without any security restrictions. There is a resource handler, a scheme handler and a resource manager. In CEF Python only resource handler is currently available. There is the wxpython-response.py example on README-Examples.md page.
资源管理器是用于加载各种内容的非常简单的API,它将在Issue#418中实现(欢迎PR): https://github.com/cztomczak/cefpython/issues/418
Resource manager is a very easy API for loading various content, it is to be implemented in Issue #418 (PR is welcome):https://github.com/cztomczak/cefpython/issues/418
有关方案处理程序,请参阅问题#50: https://github.com/cztomczak/cefpython/issues/50
For scheme handler see Issue #50:https://github.com/cztomczak/cefpython/issues/50
此外,上游CEF中还有GetResourceResponseFilter,这比资源处理程序更容易选择,可以通过问题#229实施: https://github.com/cztomczak/cefpython/issues/229
Additionally there is also GetResourceResponseFilter in upstream CEF which is an easier option than resource handler, to be implemented via Issue #229:https://github.com/cztomczak/cefpython/issues/229
您还可以在应用程序内部运行内部Web服务器(易于使用Python)并以这种方式提供文件.上游CEF还具有内置的Web服务器功能,但是我不认为这会在cefpython中公开,因为在Python中设置Web服务器已经很容易.
You could also run an internal web server inside your app (easy to do with Python) and serve files that way. Upstream CEF also has a built-in web server functionality, however I don't think this will be exposed in cefpython, as it's already easy to set up web server in Python.
这篇关于带有本地文件系统中的html/js文件的Cefpython应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!