6

I'm trying to make a hybrid python-js application with cefpython.

I would like to have:

  • JS and HTML files local to the cef python app (e.g. in './html', './js', etc)
  • Load one of the HTML files as the initial page
  • Avoid any CORS issues with files accessing each other (e.g. between directories)

The following seems to work to load the first page:

browser = cef.CreateBrowserSync(url='file:///html/index.html',
                                window_title="Rulr 2.0")

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?

Elliot Woods
  • 834
  • 11
  • 20

1 Answers1

2

Try passing "disable-web-security" switch to cef.Initialize or set BrowserSettings.web_security_disabled.

Try also setting BrowserSettings.file_access_from_file_urls_allowed and BrowserSettings.universal_access_from_file_urls_allowed.

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.

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

For scheme handler see Issue #50: https://github.com/cztomczak/cefpython/issues/50

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

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.

Czarek Tomczak
  • 20,079
  • 5
  • 49
  • 56
  • Thank you for the answer! A few notes... Many users have found Python's built-in web server to be unusably slow (see https://stackoverflow.com/questions/12905426/what-is-a-faster-alternative-to-pythons-http-server-or-simplehttpserver note that none of the recommended replacements are python based). This may be a good reason for exposing CEF's web server. – Elliot Woods Aug 04 '18 at 12:36
  • Looking at https://github.com/cztomczak/cefpython/blob/cefpython31/cefpython/cef3/linux/binaries_64bit/wxpython-response.py - It seems at line 70 that we could intercept here and serve loaded files from disk, correct? I'm not convinced that this approach is particularly speedy, but i can see how it bypasses having a web server. – Elliot Woods Aug 04 '18 at 12:41
  • Resource Manager API really looks like the way to go, with its simple 'Directory provider'. Perhaps best is to use a web server for the time being, and switch to Resource Manager API later once it becomes available. Thank you! – Elliot Woods Aug 04 '18 at 12:43
  • 1
    @ElliotWoods Created Issue [#445](https://github.com/cztomczak/cefpython/issues/445) for exposing CEF web server API. – Czarek Tomczak Aug 04 '18 at 13:36
  • 1
    @ElliotWoods Line 70 (_OnResourceResponse) is a final function for reading/modifying web content. To read contents from disk you have to modify ResourceHandler class (ProcessRequest, GetResponseHeaders, ReadResponse). Get rid of WebRequestClient which is only needed when making http requests. – Czarek Tomczak Aug 04 '18 at 13:39
  • 1
    @ElliotWoods When using resource handler for reading from disk, urls could be like: `http://filesystem/c/myapp/file.html`. – Czarek Tomczak Aug 04 '18 at 13:40