PDA

View Full Version : Transferring session & auth data when QWebView calls createWindow()



admoore
13th July 2012, 04:54
Hello, I'm new to these forums. I have posted this question to the PyQT mailing list several weeks ago, but got no response whatsoever, so I thought I'd try here.

I have a web browser application written in PyQt4 which uses a subclassed QWebView. If I load a URL that is password protected, or uses a web session (such as php session) for security, and then subsequently click a link that opens a new window (e.g. target="_blank" or javascript window.open()), the new window opens without authentication or session data and a 403 error results.

The following code illustrates this problem by opening a password-protected page on my website where you can click a link with "target=_blank" set.



from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys

class mybrowser(QWebView):

def __init__(self, parent=None):
super(mybrowser, self).__init__(parent)
url = QUrl ("http://www.alandmoore.com/wwwtest")
url.setUserName("test")
url.setPassword("test123")
self.load(url)

def createWindow(self, type):
self.w = mybrowser()
self.w.show()
return self.w

if __name__ == '__main__':
app = QApplication(sys.argv)
w = mybrowser()
w.show()
app.exec_()


How can I get new windows to retain the security credentials of the parent window?

wysota
13th July 2012, 09:46
The easiest way would probably be to share the network access manager instance between the two windows.

admoore
14th July 2012, 04:54
Thanks for your help. I tried changing the createWindow method to:


def createWindow(self, type):
self.w = mybrowser()
self.w.page().setNetworkAccessManager(self.page(). networkAccessManager())
self.w.show()
return self.w


But that causes a segfault. What's the basic MO for sharing a networkAccessManager?

Added after 1 1:

Ok, I think I got it figured out; this seems to work:



from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
import sys

class mybrowser(QWebView):

def __init__(self, parent=None, nam=None):
super(mybrowser, self).__init__(parent)
self.nam = nam or QNetworkAccessManager()
self.page().setNetworkAccessManager(self.nam)
url = QUrl ("http://www.alandmoore.com/wwwtest")
url.setUserName("test")
url.setPassword("test123")
self.load(url)

def createWindow(self, type):
self.w = mybrowser(None, self.nam)
self.w.show()
return self.w

if __name__ == '__main__':
app = QApplication(sys.argv)
w = mybrowser()
w.show()
app.exec_()


Thanks for pointing me in the right direction!!