def _set_proxy(manager, server_url=None): proxy_settings = manager.get_proxy_settings() if proxy_settings.config == 'Manual': if proxy_settings.server and proxy_settings.port: proxy = QNetworkProxy(QNetworkProxy.HttpProxy, hostName=proxy_settings.server, port=int(proxy_settings.port)) if proxy_settings.authenticated: proxy.setPassword(proxy_settings.password) proxy.setUser(proxy_settings.username) QNetworkProxy.setApplicationProxy(proxy) elif proxy_settings.config == 'System': QNetworkProxyFactory.setUseSystemConfiguration(True) elif proxy_settings.config == 'Automatic': proxy_settings = manager.get_proxies(server_url) protocol = server_url.split(":")[0] proxy_server_info = urllib2.urlparse.urlparse( proxy_settings[protocol]) proxy = QNetworkProxy(QNetworkProxy.HttpProxy, hostName=proxy_server_info.hostname, port=proxy_server_info.port) QNetworkProxy.setApplicationProxy(proxy) else: QNetworkProxy.setApplicationProxy( QNetworkProxy(QNetworkProxy.NoProxy))
def configureProxy(self, host, port, user=None, password=None): """Add a proxy configuration for the Network Requests. :param host: the proxy host :param port: the proxy port :param user: if the proxy has authentication this param sets the user to be used. It should be None if it's not required to access with a user :param password: if the proxy has authentication this param sets the password to be used. It should be None if it's not required to access with a password """ proxy = QNetworkProxy() proxy.setType(QNetworkProxy.HttpProxy) proxy.setHostName(host) proxy.setPort(port) if user is not None: proxy.setUser(user) if password is not None: proxy.setPassword(password) self._proxyAuth = (user, password) self.setProxy(proxy)
def set_proxy(self, string_proxy): """Set proxy [http|socks5]://username:password@hostname:port""" urlinfo = urlparse.urlparse(string_proxy) proxy = QNetworkProxy() if urlinfo.scheme == 'socks5' : proxy.setType(1) elif urlinfo.scheme == 'http' : proxy.setType(3) else : proxy.setType(2) self.manager.setProxy(proxy) return self.manager.proxy() proxy.setHostName(urlinfo.hostname) proxy.setPort(urlinfo.port) if urlinfo.username != None : proxy.setUser(urlinfo.username) else : proxy.setUser('') if urlinfo.password != None : proxy.setPassword(urlinfo.password) else : proxy.setPassword('') self.manager.setProxy(proxy) return self.manager.proxy()
def get_proxy(): """Adaption by source of Plugin Installer - Version 1.0.10""" settings = QSettings() settings.beginGroup("proxy") if settings.value("/proxyEnabled", False, type=bool): proxy = QNetworkProxy() proxyType = settings.value("/proxyType", 0, type=int) if proxyType in ["1", "Socks5Proxy"]: proxy.setType(QNetworkProxy.Socks5Proxy) elif proxyType in ["2", "NoProxy"]: proxy.setType(QNetworkProxy.NoProxy) elif proxyType in ["3", "HttpProxy"]: proxy.setType(QNetworkProxy.HttpProxy) elif proxyType in ["4", "HttpCachingProxy"] and QT_VERSION >= 0X040400: proxy.setType(QNetworkProxy.HttpCachingProxy) elif proxyType in ["5", "FtpCachingProxy"] and QT_VERSION >= 0X040400: proxy.setType(QNetworkProxy.FtpCachingProxy) else: proxy.setType(QNetworkProxy.DefaultProxy) proxy.setHostName(settings.value("/proxyHost")) proxy.setPort(settings.value("/proxyPort", type=int)) proxy.setUser(settings.value("/proxyUser")) proxy.setPassword(settings.value("/proxyPassword")) settings.endGroup() return proxy
def get_proxy(): """ Get a proxy according to QSettings @return: proxy @rtype: QNetworkProxy """ s = QSettings() if s.value('proxy/proxyEnabled', '') == 'true': proxy_type = s.value('proxy/proxyType', '') proxy_host = s.value('proxy/proxyHost', '') proxy_port = s.value('proxy/proxyPort', '') proxy_user = s.value('proxy/proxyUser', '') proxy_password = s.value('proxy/proxyPassword', '') proxy = QNetworkProxy() if proxy_type == 'DefaultProxy': proxy.setType(QNetworkProxy.DefaultProxy) elif proxy_type == 'Socks5Proxy': proxy.setType(QNetworkProxy.Socks5Proxy) elif proxy_type == 'HttpProxy': proxy.setType(QNetworkProxy.HttpProxy) elif proxy_type == 'HttpCachingProxy': proxy.setType(QNetworkProxy.HttpCachingProxy) elif proxy_type == 'FtpCachingProxy': proxy.setType(QNetworkProxy.FtpCachingProxy) proxy.setHostName(proxy_host) proxy.setPort(int(proxy_port)) proxy.setUser(proxy_user) proxy.setPassword(proxy_password) return proxy else: return None
def configure_proxy(self): proxy = urlparse(os.environ.get('http_proxy') or os.environ.get('HTTP_PROXY')) if proxy.hostname is not None and proxy.port is not None: q_network_proxy = QNetworkProxy(QNetworkProxy.HttpProxy, proxy.hostname, proxy.port) if proxy.username is not None: q_network_proxy.setUser(proxy.username) if proxy.password is not None: q_network_proxy.setPassword(proxy.password) QNetworkProxy.setApplicationProxy(q_network_proxy)
def setTempProxy(self, host, port=None, username=None, password=None): proxy = QNetworkProxy() proxy.setType(QNetworkProxy.HttpProxy) if username: proxy.setUser(username) if password: proxy.setPassword(password) if port: proxy.setPort(port) proxy.setHostName(host) self.page().networkAccessManager().setProxy(proxy)
def _set_proxy(self, manager): proxy_settings = manager.get_proxy_settings() if proxy_settings.config == 'Manual': if proxy_settings.server and proxy_settings.port: proxy = QNetworkProxy(QNetworkProxy.HttpProxy, hostName=proxy_settings.server, port=int(proxy_settings.port)) if proxy_settings.authenticated: proxy.setPassword(proxy_settings.password) proxy.setUser(proxy_settings.username) QNetworkProxy.setApplicationProxy(proxy) elif proxy_settings.config == 'System': QNetworkProxyFactory.setUseSystemConfiguration(True) else: QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.NoProxy))
def prox(self,mode,user,password,host,port): proxy = QNetworkProxy() if mode == "DefaultProxy": proxy.setType(QNetworkProxy.DefaultProxy) elif mode == "Socks5Proxy": proxy.setType(QNetworkProxy.Socks5Proxy) proxy.setHostName(host) proxy.setPort(int(port)) proxy.setUser(self.usern) proxy.setPassword(self.passw) QNetworkProxy.setApplicationProxy(proxy) elif mode == "HttpProxy": proxy.setType(QNetworkProxy.HttpProxy) proxy.setHostName(host) proxy.setPort(int(port)) proxy.setUser(self.usern) proxy.setPassword(self.passw) QNetworkProxy.setApplicationProxy(proxy) elif mode == "HttpCachingProxy": proxy.setType(QNetworkProxy.HttpCachingProxy) proxy.setHostName(host) proxy.setPort(int(port)) proxy.setUser(self.usern) proxy.setPassword(self.passw) QNetworkProxy.setApplicationProxy(proxy) elif mode == "FtpCachingProxy": proxy.setType(QNetworkProxy.FtpCachingProxy) proxy.setHostName(host) proxy.setPort(int(port)) proxy.setUser(self.usern) proxy.setPassword(self.passw) QNetworkProxy.setApplicationProxy(proxy)
def applySettings(self): if self.feedView.siteServer.session.user != str(self.ffNickname.text()) or self.feedView.siteServer.session.remoteKey != str(self.ffRemoteKey.text()): self.feedView.siteServer.session.user = str(self.ffNickname.text()) self.feedView.siteServer.session.remoteKey = str(self.ffRemoteKey.text()) self.feedView.goHome() proxy = QNetworkProxy() proxyType = self.proxyType.currentIndex() proxy.setType( (proxyType == 2) and QNetworkProxy.Socks5Proxy or ( (proxyType == 1) and QNetworkProxy.HttpProxy or QNetworkProxy.NoProxy ) ) if proxyType: proxy.setHostName(self.proxyHost.text()) proxy.setPort(self.proxyPort.value()) if self.proxyAuth.checkState() == Qt.Checked: proxy.setUser(self.proxyAuthUser.text()) proxy.setPassword(self.proxyAuthPassword.text()) QNetworkProxy.setApplicationProxy(proxy)
def get_proxy(): """Adaption by source of Plugin Installer - Version 1.0.10 :returns: Get proxy that currently used. :rtype: QNetworkProxy """ settings = QSettings() settings.beginGroup('proxy') enabled = settings.value('/proxyEnabled', False, type=bool) if not enabled: return None proxy = QNetworkProxy() try: proxy_type = settings.value('/proxyType', 0, type=int) except TypeError: # Catch for this: # TypeError: unable to convert a QVariant of type 10 to a # QMetaType of type 2 # TODO: can we do anything to handle this more gracefully? TS settings.setValue('/proxyType', 0) return proxy if proxy_type in ['1', 'Socks5Proxy']: proxy.setType(QNetworkProxy.Socks5Proxy) elif proxy_type in ['2', 'NoProxy']: proxy.setType(QNetworkProxy.NoProxy) elif proxy_type in ['3', 'HttpProxy']: proxy.setType(QNetworkProxy.HttpProxy) elif proxy_type in ['4', 'HttpCachingProxy'] and QT_VERSION >= 0X040400: proxy.setType(QNetworkProxy.HttpCachingProxy) elif proxy_type in ['5', 'FtpCachingProxy'] and QT_VERSION >= 0X040400: proxy.setType(QNetworkProxy.FtpCachingProxy) else: proxy.setType(QNetworkProxy.DefaultProxy) proxy.setHostName(settings.value('/proxyHost')) proxy.setPort(settings.value('/proxyPort', type=int)) user = settings.value('/proxyUser', type=str) password = settings.value('/proxyPassword', type=str) if user is not None: proxy.setUser(user) if password is not None and user is not None: proxy.setPassword(password) settings.endGroup() return proxy
def configure_proxy(cls, hostname, port, user=None, password=None, proxy_type=QNetworkProxy.HttpProxy): """ Configure network proxy layer. @param proxy_type: see QNetworkProxy.ProxyType. Default: HttpProxy. @param hostname: Proxy hostname. @param port: Proxy port. @param username: Proxy username (optional). @param passwrod: Proxy password (optional). """ proxy = QNetworkProxy() proxy.setType(proxy_type) proxy.setHostName(hostname) proxy.setPort(port) if user and password is not None: proxy.setUser(user) proxy.setPassword(password) QNetworkProxy.setApplicationProxy(proxy);
def _set_proxy(self, manager, server_url=None): proxy_settings = manager.get_proxy_settings() if proxy_settings.config == 'Manual': if proxy_settings.server and proxy_settings.port: proxy = QNetworkProxy(QNetworkProxy.HttpProxy, hostName=proxy_settings.server, port=int(proxy_settings.port)) if proxy_settings.authenticated: proxy.setPassword(proxy_settings.password) proxy.setUser(proxy_settings.username) QNetworkProxy.setApplicationProxy(proxy) elif proxy_settings.config == 'System': QNetworkProxyFactory.setUseSystemConfiguration(True) elif proxy_settings.config == 'Automatic': proxy_settings = manager.get_proxies(server_url) protocol = server_url.split(":")[0] proxy_server_info = urllib2.urlparse.urlparse(proxy_settings[protocol]) proxy = QNetworkProxy(QNetworkProxy.HttpProxy, hostName=proxy_server_info.hostname, port=proxy_server_info.port) QNetworkProxy.setApplicationProxy(proxy) else: QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.NoProxy))
def configure_proxy(cls, hostname, port, user=None, password=None, proxy_type=QNetworkProxy.HttpProxy): """ Configure network proxy layer. @param proxy_type: see QNetworkProxy.ProxyType. Default: HttpProxy. @param hostname: Proxy hostname. @param port: Proxy port. @param username: Proxy username (optional). @param passwrod: Proxy password (optional). """ proxy = QNetworkProxy() proxy.setType(proxy_type) proxy.setHostName(hostname) proxy.setPort(port) if user and password is not None: proxy.setUser(user) proxy.setPassword(password) QNetworkProxy.setApplicationProxy(proxy)
if proxy_socket.error() != QTcpSocket.RemoteHostClosedError: url = proxy_socket.property('url').toUrl() error_string = proxy_socket.errorString() if self.debug: self.log.write('Error for %s %s\n\n' % (url, error_string)) proxy_socket.deleteLater() if __name__ == '__main__': app = QCoreApplication(sys.argv) server = HTTPProxy() manager = QNetworkAccessManager() manager.finished.connect(server.stopServing) manager.finished.connect(app.quit) proxy = QNetworkProxy() proxy.setType(QNetworkProxy.HttpProxy) proxy.setHostName('127.0.0.1') proxy.setPort(server.port()) proxy.setUser(server.username) proxy.setPassword(server.password) manager.setProxy(proxy) reply = manager.get(QNetworkRequest(QUrl('http://aws.amazon.com/'))) sys.exit(app.exec_())
def __init__(self, parent = None): QMainWindow.__init__(self, parent) self.setWindowTitle("FeedLol") self.aboutAction = QAction(QIcon("data/icons/help-about.svg"), "&About FeedLol...", self) self.connect(self.aboutAction, SIGNAL("triggered()"), self.slotAbout) self.reloadAction = QAction(QIcon("data/icons/view-refresh.svg"), "&Reload", self) self.reloadAction.setShortcut(QKeySequence.Refresh) self.homeAction = QAction(QIcon("data/icons/go-home.svg"), "Go &Home", self) self.homeAction.setShortcut("Alt+Home") self.userAction = QAction(QIcon("data/icons/user-identity.svg"), "&Me", self) self.userAction.setShortcut("Ctrl+M") self.logoutAction = QAction(QIcon("data/icons/dialog-close.svg"), "Log&out", self) self.logoutAction.setShortcut(QKeySequence.Close) self.settingsAction = QAction(QIcon("data/icons/configure.svg"), "&Preferences...", self) self.connect(self.settingsAction, SIGNAL("triggered()"), self.slotSettings) self.toolbar = QToolBar("Toolbar", self) self.toolbar.setObjectName("toolbar") self.toolbar.addAction(self.homeAction) self.toolbar.addAction(self.userAction) self.toolbar.addAction(self.reloadAction) self.toolbar.addSeparator() self.toolbar.addAction(self.logoutAction) self.toolbar.addSeparator() self.toolbar.addAction(self.settingsAction) self.toolbar.addAction(self.aboutAction) self.addToolBar(self.toolbar) self.loadStatus = QProgressBar(self) self.loadStatus.setRange(0, 100) self.loadStatus.setMaximumWidth(200) self.loadStatus.setTextVisible(False) self.loadStatus.hide() self.statusBar().addPermanentWidget(self.loadStatus) self.feedView = FeedView(self) self.setCentralWidget(self.feedView) self.connect(self.feedView, SIGNAL("titleChanged(const QString&)"), self.slotSetTitle) self.connect(self.feedView, SIGNAL("statusBarMessage(const QString&)"), self.statusBar(), SLOT("showMessage(const QString&)")) self.connect(self.feedView, SIGNAL("loadStarted()"), self.loadStart) self.connect(self.feedView, SIGNAL("loadFinished(bool)"), self.loadStop) self.connect(self.feedView, SIGNAL("loadProgress(int)"), self.loadProgress) self.connect(self.reloadAction, SIGNAL("triggered()"), self.feedView.reload) self.connect(self.homeAction, SIGNAL("triggered()"), self.feedView.goHome) self.connect(self.userAction, SIGNAL("triggered()"), self.feedView.goToUserPage) self.connect(self.logoutAction, SIGNAL("triggered()"), self.feedView.logout) self.connect(self.feedView.page(), SIGNAL("linkHovered(const QString&, const QString&, const QString&)"), self.linkHovered) self.settingsDialog = SettingsDialog(self.feedView, self) settings = QSettings() if settings.contains("proxy/type"): proxy = QNetworkProxy() proxyType = settings.value("proxy/type").toInt()[0] proxy.setType( (proxyType == 2) and QNetworkProxy.Socks5Proxy or ( (proxyType == 1) and QNetworkProxy.HttpProxy or QNetworkProxy.NoProxy ) ) if proxy.type() != QNetworkProxy.NoProxy: proxy.setHostName(settings.value("proxy/host").toString()) proxy.setPort(settings.value("proxy/port").toInt()[0]) if settings.value("proxy/user").toString(): proxy.setUser(settings.value("proxy/user").toString()) proxy.setPassword(settings.value("proxy/password").toString()) QNetworkProxy.setApplicationProxy(proxy) if settings.contains("mainWindow/geometry"): self.restoreGeometry(settings.value("mainWindow/geometry").toByteArray()) else: self.resize(320,480) if settings.contains("mainWindow/state"): self.restoreState(settings.value("mainWindow/state").toByteArray()) self.feedView.goHome()
current = home s = QSettings() for key, val in settings.iteritems(): settings_key = key for key2, val2 in current.iteritems(): if key2 == settings_key: settings_val = val2 current_setting = s.value(str(val).decode('unicode-escape')) s.setValue(unicode(str(val)), settings_val) s.sync() proxyEnabled = s.value("proxy/proxyEnabled", "") proxyType = s.value("proxy/proxyType", "") proxyHost = s.value("proxy/proxyHost", "") proxyPort = s.value("proxy/proxyPort", "") proxyUser = s.value("proxy/proxyUser", "") proxyPassword = s.value("proxy/proxyPassword", "") proxy = QNetworkProxy() proxy.setType(QNetworkProxy.HttpProxy) proxy.setHostName(proxyHost) proxy.setPort(int(proxyPort)) proxy.setUser(proxyUser) proxy.setPassword(proxyPassword) QNetworkProxy.setApplicationProxy(proxy) net_man = QgsNetworkAccessManager.instance() stringlist = "" net_man.setupDefaultProxyAndCache() net_man.setFallbackProxyAndExcludes(proxy, stringlist)
socket.disconnectFromHost() if proxy_socket.error() != QTcpSocket.RemoteHostClosedError: url = proxy_socket.property('url').toUrl() error_string = proxy_socket.errorString() if self.debug: self.log.write('Error for %s %s\n\n' % (url, error_string)) proxy_socket.deleteLater() if __name__ == '__main__': app = QCoreApplication(sys.argv) server = HTTPProxy() manager = QNetworkAccessManager() manager.finished.connect(server.stopServing) manager.finished.connect(app.quit) proxy = QNetworkProxy() proxy.setType(QNetworkProxy.HttpProxy) proxy.setHostName('127.0.0.1') proxy.setPort(server.port()) proxy.setUser(server.username) proxy.setPassword(server.password) manager.setProxy(proxy) reply = manager.get(QNetworkRequest(QUrl('http://aws.amazon.com/'))) sys.exit(app.exec_())