pyqt5.6 interceptRequest doesn't work

2019-09-21 06:01发布

问题:

I want to interceptor a url request to another by subclass QWebEngineUrlRequestInterceptor:

class RequestInterceptor(QWebEngineUrlRequestInterceptor): 
    def interceptRequest(self,info): 
        print('#################interceptRequest')
        print(info.requestUrl(),info.firstPartyUrl(),info.NavigationType,info.resourceType(),info.requestMethod())
        if info.requestUrl().endswith("/jquery.js"):
           info.redirect('/jqueryTest.js')



app = QApplication([]) 
p = QWebEnginePage() 
v = QWebEngineView() 
v.setPage(p) 
p.profile().setRequestInterceptor(RequestInterceptor())
c.registerObject('bridge', p)
url = "http://127.0.0.1:8000/test.html?t=5"
v.setUrl(QUrl(url)) 
v.show() 
app.exec_()

When I run the code,the interceptor does not work!
Hope someone give me help,thanks!

PS: May It is caused by python garbage collection。So I store the interceptor in varible by modifying the code

p.profile().setRequestInterceptor(RequestInterceptor())

to

interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )

That's All.

回答1:

May It is caused by python garbage collection。So I store the interceptor in varible by modifying the code

p.profile().setRequestInterceptor(RequestInterceptor())

to

interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )

That's All.