pyqt5.6 interceptRequest doesn't work

谁说胖子不能爱 提交于 2019-12-12 07:00:57

问题


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.



来源:https://stackoverflow.com/questions/37658772/pyqt5-6-interceptrequest-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!